• C# 图片处理


    按比例调整图片尺寸

     1 #region 按比例调整图片尺寸
     2 public static Bitmap GetThumbnail(Image bmp, int width, int height)
     3 {
     4     if (width == 0)
     5     {
     6         width = height * bmp.Width / bmp.Height;
     7     }
     8     if (height == 0)
     9     {
    10         height = width * bmp.Height / bmp.Width;
    11     }
    12     Image imgSource = bmp;
    13     Bitmap outBmp = new Bitmap(width, height);
    14     Graphics g = Graphics.FromImage(outBmp);
    15     g.Clear(Color.Transparent);
    16     // 设置画布的描绘质量   
    17     g.CompositingQuality = CompositingQuality.HighQuality;
    18     g.SmoothingMode = SmoothingMode.HighQuality;
    19     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    20 
    21     g.DrawImage(imgSource, new Rectangle(0, 0, width, height + 1), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
    22 
    23     g.Dispose();
    24     imgSource.Dispose();
    25     bmp.Dispose();
    26     return outBmp;
    27 }
    28 #endregion
    按比例调整图片尺寸

      

    合成图片

     1 /// <summary>
     2 /// 两种图片合并,类似相册,有个背景图,中间贴自己的目标图片        
     3 /// <param name="sourceImg">粘贴的源图片</param>
     4 /// <param name="destImg">粘贴的目标图片</param>
     5 /// <param name="name">合成图片存储路径</param>
     6 /// </summary>
     7 public void CombinImage(string sourceImg, string destImg, string name)
     8 {
     9     System.Drawing.Image imgBack = System.Drawing.Image.FromFile(sourceImg);     //相框图片 
    10     System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);        //照片图片
    11 
    12     Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height);
    13 
    14     Graphics g = Graphics.FromImage(bmp);
    15     g.Clear(Color.White);
    16     g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);
    17     //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
    18 
    19     //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
    20     g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + 0, imgBack.Height / 2 - img.Height / 2 + 0, img.Width, img.Height);
    21     GC.Collect();
    22 
    23     //保存指定格式图片
    24     bmp.Save(name, ImageFormat.Png);    
    25 }
    合成图片

     合成前:

    合成后:

  • 相关阅读:
    ASP.NET Core MVC Razor小记
    ASP.NET Core引入第三方日志框架及简单实现日志策略配置
    test
    记录一个Windows explorer进程卡死的处理,有关于“MicrosoftWindows.Client.CBS_cw5n1h2txyewy”的,已解决!
    Windows版本sed工具
    相同xml批量创建替换脚本.sh
    springboot1.x apollo 更改属性值不起作用。 ConfigurationProperties
    jmeter固定定时器
    jmeter函数助手参数化
    jmeter循环控制器
  • 原文地址:https://www.cnblogs.com/Swaggy-yyq/p/15036014.html
Copyright © 2020-2023  润新知