• 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 }
    合成图片

     合成前:

    合成后:

  • 相关阅读:
    分享上大学时CCTV5经常播放的一段宣传片
    嗯, 在Vista下面post一篇, 快过年的二三事
    [转自天涯]很多年以前,我是一个中锋
    关于Anthem的Button控件, 为啥仍然会PostBack?
    小白三下杭州
    搜狗, 谷歌, 紫光, 3大输入法互打结果.
    2008春节回家流水账
    早上拍的雪景.
    如果安装.net framework 3.5出错, 可以这样解决
    我那幸福的坐车偶记...
  • 原文地址:https://www.cnblogs.com/Swaggy-yyq/p/15036014.html
Copyright © 2020-2023  润新知