WPF(C#)中Bitmap与BitmapImage相互转换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了WPF(C#)中Bitmap与BitmapImage相互转换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2582字,纯文字阅读大概需要4分钟。
内容图文
![WPF(C#)中Bitmap与BitmapImage相互转换](/upload/InfoBanner/zyjiaocheng/650/8d2f502da3e84afd9871898e8082d8f1.jpg)
一、WPF的Image控件中设置ImageSource
image1.Source = new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));
还可以使用:
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); ms.Dispose(); image1.Source = bitmapImage;
还可以使用:
BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径 bitmapImage.EndInit(); bitmapImage.Freeze(); image1.Source = bitmapImage;
二、Bitmap转BitmapImage
先将Bitmap储存成memorystream,然后指定给BitmapImage
private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { bitmap.Save(ms, bitmap.RawFormat); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); } return bitmapImage; } image1.Source = BitmapToBitmapImage(bitmap);
三、Bitmap转BitmapSource
/*------------------------------------------------------------------------- //Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针, //返回一个托管的BitmapSource ---------------------------------------------------------------------------*/ Bitmap bitmap = CaptureScreen.GetDesktopImage(); IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象 BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); image1.Source = bitmapSource;
四、BitmapSource转Bitmap
BitmapSource m = (BitmapSource)image1.Source; System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); System.Drawing.Imaging.BitmapData data = bmp.LockBits( new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);
内容总结
以上是互联网集市为您收集整理的WPF(C#)中Bitmap与BitmapImage相互转换全部内容,希望文章能够帮你解决WPF(C#)中Bitmap与BitmapImage相互转换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。