zoukankan      html  css  js  c++  java
  • C# 绘制图片平铺,拉伸,居中等(转)

    自从用C#,发现自己变的越来越懒,C#各个功能实现都可以在网上找到类似代码。

    我就乐的坐享其成,把代码改改就OK了。

    代码出自:http://www.dylike-soft.com/blogview.asp?id=109

    C# 源码

    /// <summary>
    /// 填充模式
    /// </summary>
    /// <remarks></remarks>
    public enum FillMode
    {
       
    /// <summary>
       
    /// 平铺
       
    /// </summary>
       
    /// <remarks></remarks>
        Title = 0,
       
    /// <summary>
       
    /// 居中
       
    /// </summary>
       
    /// <remarks></remarks>
        Center = 1,
       
    /// <summary>
       
    /// 拉伸
       
    /// </summary>
       
    /// <remarks></remarks>
        Struk = 2,
       
    /// <summary>
       
    /// 缩放
       
    /// </summary>
       
    /// <remarks></remarks>
        Zoom = 3
    }
    /// <summary>
    /// 将指向图像按指定的填充模式绘制到目标图像上
    /// </summary>
    /// <param name="SourceBmp">要控制填充模式的源图</param>
    /// <param name="TargetBmp">要绘制到的目标图</param>
    /// <param name="_FillMode">填充模式</param>
    /// <remarks></remarks>
    public void Image_FillRect(Bitmap SourceBmp, Bitmap TargetBmp, FillMode _FillMode)
    {
       
    try {
           
    switch (_FillMode) {
               
    case FillMode.Title:
                   
    using (TextureBrush Txbrus = new TextureBrush(SourceBmp)) {
                        Txbrus.WrapMode
    = Drawing2D.WrapMode.Tile;
                       
    using (Graphics G = Graphics.FromImage(TargetBmp)) {
                            G.FillRectangle(Txbrus,
    new Rectangle(0, 0, TargetBmp.Width - 1, TargetBmp.Height - 1));
                        }
                    }

                   
    break;
               
    case FillMode.Center:
                   
    using (Graphics G = Graphics.FromImage(TargetBmp)) {
                       
    int xx = (TargetBmp.Width - SourceBmp.Width) / 2;
                       
    int yy = (TargetBmp.Height - SourceBmp.Height) / 2;
                        G.DrawImage(SourceBmp,
    new Rectangle(xx, yy, SourceBmp.Width, SourceBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
                    }

                   
    break;
               
    case FillMode.Struk:
                   
    using (Graphics G = Graphics.FromImage(TargetBmp)) {
                        G.DrawImage(SourceBmp,
    new Rectangle(0, 0, TargetBmp.Width, TargetBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
                    }

                   
    break;
               
    case FillMode.Zoom:
                   
    double tm = 0.0;
                   
    int W = SourceBmp.Width;
                   
    int H = SourceBmp.Height;
                   
    if (W > TargetBmp.Width) {
                        tm
    = TargetBmp.Width / SourceBmp.Width;
                        W
    = W * tm;
                        H
    = H * tm;
                    }
                   
    if (H > TargetBmp.Height) {
                        tm
    = TargetBmp.Height / H;
                        W
    = W * tm;
                        H
    = H * tm;
                    }
                   
    using (Bitmap tmpBP = new Bitmap(W, H)) {
                       
    using (Graphics G2 = Graphics.FromImage(tmpBP)) {
                            G2.DrawImage(SourceBmp,
    new Rectangle(0, 0, W, H), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
                           
    using (Graphics G = Graphics.FromImage(TargetBmp)) {
                               
    int xx = (TargetBmp.Width - W) / 2;
                               
    int yy = (TargetBmp.Height - H) / 2;
                                G.DrawImage(tmpBP,
    new Rectangle(xx, yy, W, H), new Rectangle(0, 0, W, H), GraphicsUnit.Pixel);
                            }
                        }
                    }

                   
    break;
            }
        }
    catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }

  • 相关阅读:
    10-tensorflow-tf.concat()
    09-tensorflow-tf.split()
    10-numpy笔记-np.random.randint
    学习网络编程的一些实用技巧和细节
    读书笔记_Effective_C++_条款三十一:将文件间的编译依存关系降至最低(第一部分) 重新学习了 继续学习第二 三部分更加精彩
    对四次挥手中的TIME_WAIT状态的学习
    accept 和 connect API深入 重点accept阻塞和非阻塞问题学习
    几种IO情况的学习和总结 关于 =====阻塞/非阻塞以及同步/异步区别
    tcp头和ip头 图文简介和简要说明
    Nginx 为什么要延迟关闭
  • 原文地址:https://www.cnblogs.com/Yjianyong/p/2024378.html
Copyright © 2011-2022 走看看