国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

WPF實現圖片合成或加水印的方法【2種方法】

2019-10-29 21:12:37
字體:
來源:轉載
供稿:網友

本文實例講述了WPF實現圖片合成或加水印的方法。分享給大家供大家參考,具體如下:

最近項目中應用多次應用了圖片合成,為了今后方便特此記下。

在WPF下有兩種圖片合成的方式,一種還是用原來C#提供的GDI+方式,命名空間是System.Drawing 和 System.Drawing.Imaging,另一種是WPF中新添加的API,命名空間是 System.Windows.Media 和 System.Windows.Media.Imaging 。

我們來做一個簡單的例子,分別用上面的兩種方式實現,功能是在一個背景圖上面,畫一個頭像,然后在寫一個簽名。

首先準備一張背景圖(bg.jpg)和兩個頭像圖片(tiger.png 和 lion.png)最后的生成的圖片效果如下圖:

WPF,圖片合成,加水印

WPF,圖片合成,加水印

把準備的素材拷貝到項目中,其文件屬性【復制到輸出目錄】選擇【始終復制】,【生成操作】選擇【內容】。

這里貼一下兩種方式的主要代碼,具體代碼可以在文章最后點擊下載。

第一種方式

使用RenderTargetBitmap 和 DrawingVisual 方式

private BitmapSource MakePicture(string bgImagePath, string headerImagePath, string signature){  //獲取背景圖  BitmapSource bgImage = new BitmapImage(new Uri(bgImagePath, UriKind.Relative));  //獲取頭像  BitmapSource headerImage = new BitmapImage(new Uri(headerImagePath, UriKind.Relative));  //創建一個RenderTargetBitmap 對象,將WPF中的Visual對象輸出  RenderTargetBitmap composeImage = new RenderTargetBitmap(bgImage.PixelWidth, bgImage.PixelHeight, bgImage.DpiX, bgImage.DpiY, PixelFormats.Default);  FormattedText signatureTxt = new FormattedText(signature,      System.Globalization.CultureInfo.CurrentCulture,      System.Windows.FlowDirection.LeftToRight,      new Typeface(System.Windows.SystemFonts.MessageFontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),      50,      System.Windows.Media.Brushes.White);  DrawingVisual drawingVisual = new DrawingVisual();  DrawingContext drawingContext = drawingVisual.RenderOpen();  drawingContext.DrawImage(bgImage, new Rect(0, 0, bgImage.Width, bgImage.Height));  //計算頭像的位置  double x = (bgImage.Width / 2 - headerImage.Width) / 2;  double y = (bgImage.Height - headerImage.Height) / 2 - 100;  drawingContext.DrawImage(headerImage, new Rect(x, y, headerImage.Width, headerImage.Height));  //計算簽名的位置  double x2 = (bgImage.Width/2 - signatureTxt.Width) / 2;  double y2 = y + headerImage.Height + 20;  drawingContext.DrawText(signatureTxt, new System.Windows.Point(x2, y2));  drawingContext.Close();  composeImage.Render(drawingVisual);  //定義一個JPG編碼器  JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder();  //加入第一幀  bitmapEncoder.Frames.Add(BitmapFrame.Create(composeImage));  //保存至文件(不會修改源文件,將修改后的圖片保存至程序目錄下)  string savePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/合成.jpg";  bitmapEncoder.Save(File.OpenWrite(Path.GetFileName(savePath)));  return composeImage;}

第二種方式

利用原來的GDI+方式

private BitmapSource MakePictureGDI(string bgImagePath, string headerImagePath, string signature){  GDI.Image bgImage = GDI.Bitmap.FromFile(bgImagePath);  GDI.Image headerImage = GDI.Bitmap.FromFile(headerImagePath);  //新建一個畫板,畫板的大小和底圖一致  System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bgImage.Width, bgImage.Height);  System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);  //設置高質量插值法  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  //設置高質量,低速度呈現平滑程度  g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  //清空畫布并以透明背景色填充  g.Clear(System.Drawing.Color.Transparent);  //先在畫板上面畫底圖  g.DrawImage(bgImage, new GDI.Rectangle(0, 0, bitmap.Width, bitmap.Height));  //再在畫板上畫頭像  int x = (bgImage.Width / 2 - headerImage.Width) / 2;  int y = (bgImage.Height - headerImage.Height) / 2 - 100;  g.DrawImage(headerImage, new GDI.Rectangle(x, y, headerImage.Width, headerImage.Height),               new GDI.Rectangle(0, 0, headerImage.Width, headerImage.Height),               GDI.GraphicsUnit.Pixel);  //在畫板上寫文字  using (GDI.Font f = new GDI.Font("Arial", 20, GDI.FontStyle.Bold))  {    using (GDI.Brush b = new GDI.SolidBrush(GDI.Color.White))    {      float fontWidth = g.MeasureString(signature, f).Width;      float x2 = (bgImage.Width / 2 - fontWidth) / 2;      float y2 = y + headerImage.Height + 20;      g.DrawString(signature, f, b, x2, y2);    }  }  try  {    string savePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/GDI+合成.jpg";    bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);    return ToBitmapSource(bitmap);  }  catch (System.Exception e)  {    throw e;  }  finally  {    bgImage.Dispose();    headerImage.Dispose();    g.Dispose();  }}#region GDI+ Image 轉化成 BitmapSource[System.Runtime.InteropServices.DllImport("gdi32")]static extern int DeleteObject(IntPtr o);public BitmapSource ToBitmapSource(GDI.Bitmap bitmap){  IntPtr ip = bitmap.GetHbitmap();  BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(    ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());  DeleteObject(ip);//釋放對象  return bitmapSource;}#endregion

附:完整實例代碼點擊此處本站下載。

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺兰县| 宣化县| 湘西| 酒泉市| 高阳县| 黄龙县| 台湾省| 贺州市| 万全县| 延吉市| 靖西县| 南平市| 和政县| 锡林浩特市| 体育| 天柱县| 洪江市| 张家港市| 泾阳县| 南安市| 华亭县| 长泰县| 淄博市| 富民县| 河西区| 永定县| 攀枝花市| 颍上县| 瑞昌市| 方山县| 镶黄旗| 昌宁县| 枣庄市| 寿阳县| 山阴县| 德格县| 泾川县| 米易县| 彰化县| 共和县| 无棣县|