WPF 下两种图片合成或加水印的方式
2012-11-29 15:18
363 查看
最近项目中应用多次应用了图片合成,为了今后方便特此记下。
在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名空间是System.Drawing 和 System.Drawing.Imaging,另一种是WPF中新添加的API,命名空间是 System.Windows.Media 和 System.Windows.Media.Imaging 。
我们来做一个简单的例子,分别用上面的两种方式实现,功能是在一个背景图上面,画一个头像,然后在写一个签名。
首先准备一张背景图(bg.jpg)和两个头像图片(tiger.png 和 lion.png)最后的生成的图片效果如下图:
利用原来的GDI+方式
-==源码下载 ==-
在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名空间是System.Drawing 和 System.Drawing.Imaging,另一种是WPF中新添加的API,命名空间是 System.Windows.Media 和 System.Windows.Media.Imaging 。
我们来做一个简单的例子,分别用上面的两种方式实现,功能是在一个背景图上面,画一个头像,然后在写一个签名。
首先准备一张背景图(bg.jpg)和两个头像图片(tiger.png 和 lion.png)最后的生成的图片效果如下图:
利用原来的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
-==源码下载 ==-
相关文章推荐
- WPF 下两种图片合成或加水印的方式
- WPF 下两种图片合成或加水印的方式及资源【生成操作】
- WPF实现图片合成或加水印的方法【2种方法】
- WPF中使用Image控件上传图片的两种方式
- IOS给图片添加水印(两种方式)
- [算法]iOS 视频添加水印,合成视频两种方案(整体渲染和分割渲染)
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- Android根据图片文件名获取它的资源ID 的两种方式
- PHP 两张图片合成一张图片(加水印)
- base64转图片的两种方式
- Android中保存图片的两种方式
- iOS开发图片纯色填充(两种方式)和缩小图片
- php面向对象与面向过程两种方法给图片添加文字水印
- 用imageMagick合成图片添加图片水印
- 安卓获取图片缩略图的两种方式
- asp.net中把有图片的execl导入到数据库(以二进制流的方式存放图片)中的两种方法
- ios_图片放大的两种方式transform和frame
- Android中保存图片的两种方式
- Android 图片上传的两种方式
- 两种方式实现圆形图片