WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】
本文實(shí)例講述了WPF實(shí)現(xiàn)圖片合成或加水印的方法。分享給大家供大家參考,具體如下:
最近項(xiàng)目中應(yīng)用多次應(yīng)用了圖片合成,為了今后方便特此記下。
在WPF下有兩種圖片合成的方式,一種還是用原來(lái)C#提供的GDI+方式,命名空間是System.Drawing 和 System.Drawing.Imaging,另一種是WPF中新添加的API,命名空間是 System.Windows.Media 和 System.Windows.Media.Imaging 。
我們來(lái)做一個(gè)簡(jiǎn)單的例子,分別用上面的兩種方式實(shí)現(xiàn),功能是在一個(gè)背景圖上面,畫一個(gè)頭像,然后在寫一個(gè)簽名。
首先準(zhǔn)備一張背景圖(bg.jpg)和兩個(gè)頭像圖片(tiger.png 和 lion.png)最后的生成的圖片效果如下圖:
把準(zhǔn)備的素材拷貝到項(xiàng)目中,其文件屬性【復(fù)制到輸出目錄】選擇【始終復(fù)制】,【生成操作】選擇【內(nèi)容】。
這里貼一下兩種方式的主要代碼,具體代碼可以在文章最后點(diǎn)擊下載。
第一種方式
使用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)); //創(chuàng)建一個(gè)RenderTargetBitmap 對(duì)象,將WPF中的Visual對(duì)象輸出 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)); //計(jì)算頭像的位置 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)); //計(jì)算簽名的位置 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); //定義一個(gè)JPG編碼器 JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder(); //加入第一幀 bitmapEncoder.Frames.Add(BitmapFrame.Create(composeImage)); //保存至文件(不會(huì)修改源文件,將修改后的圖片保存至程序目錄下) string savePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\合成.jpg"; bitmapEncoder.Save(File.OpenWrite(Path.GetFileName(savePath))); return composeImage; }
第二種方式
利用原來(lái)的GDI+方式
private BitmapSource MakePictureGDI(string bgImagePath, string headerImagePath, string signature) { GDI.Image bgImage = GDI.Bitmap.FromFile(bgImagePath); GDI.Image headerImage = GDI.Bitmap.FromFile(headerImagePath); //新建一個(gè)畫板,畫板的大小和底圖一致 System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bgImage.Width, bgImage.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //設(shè)置高質(zhì)量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 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 轉(zhuǎn)化成 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);//釋放對(duì)象 return bitmapSource; } #endregion
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
本文標(biāo)題:WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5840.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改