C#将RGB图像格式数据转换为BITMAP和BITSOURCE对象

 
 1 public void RGBTOBITMAP(BYTE/*自定义的数据IntPtr*/ bits,int width/*图像的宽度*/,int height/*图像的高度*/)
 2 {
 3     int widthStep = width*3; //一般默认步长为宽度的3倍
 4     int lenData = widthStep * height;//数据长度,24位图像数据的长度=宽度*高度*3
 5     int len = 0; //代表长度
 6     //bits图像帧数据
 7     byte[] buffer = new byte[len];//创建指定长度byte数据
 8     Marshal.Copy(bits, buffer, 0, buffer.Length);
 9 
10 
11     /*****下面的就是将RGB数据转换为BITMAP对象******/
12     int stride = width * 3;
13     GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
14     int scan0 = (int)handle.AddrOfPinnedObject();
15     scan0 += (height - 1) * stride;
16     System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, -stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr)scan0);
17     handle.Free();
18     /********************************************/
19 
20     /*****下面的代码是显示到wpf的Image控件上实时显示******/
21     IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象
22 
23     BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
24     System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
25     
26     image1.Source = bitmapSource;//image1表示wpf的image控件变量
27     /********************************************/
28     bitmap.Dispose(); //bitmap释放
29 
30     /*使用DeleteObejct这个函数需要从gdi32.dll导入函数
31     [System.Runtime.InteropServices.DllImport("gdi32.dll")]
32             public static extern bool DeleteObject(IntPtr hObject);*/
33     DeleteObject(ip);
34 }

 

转载于:https://www.cnblogs.com/cyymfm1314/p/9116193.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值