I've found loads of people converting BitmapSources to Bitmaps, but what about ImageSources to Bitmaps? I am making an imaging program and I need to extract bitmaps from the image displayed in the Image element. Does anyone know how to do this?

EDIT 1:

This is a function for converting the Bitmap Image to a Bitmap. Remember to set the 'unsafe' option in the compiler preferences.

public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs)

{

System.Drawing.Bitmap btm = null;

int width = srs.PixelWidth;

int height = srs.PixelHeight;

int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);

byte[] bits = new byte[height * stride];

srs.CopyPixels(bits, stride, 0);

unsafe

{

fixed (byte* pB = bits)

{

IntPtr ptr = new IntPtr(pB);

btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr);

}

}

return btm;

}

Next is now to get a Bitmap Image:

RenderTargetBitmap targetBitmap = new RenderTargetBitmap(

(int)inkCanvas1.ActualWidth,

(int)inkCanvas1.ActualHeight,

96d, 96d,

PixelFormats.Default);

targetBitmap.Render(inkCanvas1);

MemoryStream mse = new MemoryStream();

System.Windows.Media.Imaging.BmpBitmapEncoder mem = new BmpBitmapEncoder();

mem.Frames.Add(BitmapFrame.Create(targetBitmap));

mem.Save(mse);

mse.Position = 0;

BitmapImage bi = new BitmapImage();

bi.BeginInit();

bi.StreamSource = mse;

bi.EndInit();

Next is to convert it:

Bitmap b = new Bitmap(BitmapSourceToBitmap(bi));

解决方案

Actually you don't need to use unsafe code. There's an overload of CopyPixels that accepts an IntPtr:

public static System.Drawing.Bitmap BitmapSourceToBitmap2(BitmapSource srs)

{

int width = srs.PixelWidth;

int height = srs.PixelHeight;

int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);

IntPtr ptr = IntPtr.Zero;

try

{

ptr = Marshal.AllocHGlobal(height * stride);

srs.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride);

using (var btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr))

{

// Clone the bitmap so that we can dispose it and

// release the unmanaged memory at ptr

return new System.Drawing.Bitmap(btm);

}

}

finally

{

if (ptr != IntPtr.Zero)

Marshal.FreeHGlobal(ptr);

}

}

c# image转换为bitmap,C# - 转换WPF Image.source到System.Drawing.Bitmap相关推荐

  1. C# 使用System.Drawing.Bitmap报错

    C#使用System.Drawing.Bitmap时报错 最近创建c#项目,使用System.Drawing.Bitmap时报错如下: CS1069 未能在命名空间"System.Drawi ...

  2. 让System.Drawing.Bitmap可以在linux运行

    .net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...

  3. C#遇到System.Drawing.Bitmap有问题

    C#处理bitmap数据时,遇到 error CS0246: 未能找到类型或命名空间名"Bitmap"(是否缺少 using 指令或程序集引用?) 貌似是因为缺少 System.D ...

  4. 非托管内存转换为System.Drawing.Bitmap

    底层相机采集模块使用C++编写,需要传递给上层UI(C#)显示,即将非托管内存转换为Bitmap: C#与C++接口图像表示: /// <summary> /// 底层图像表示 /// & ...

  5. RGB和Bitmap互相转换

    RGB和Bitmap互相转换 2017年05月09日 21:48:44 阅读数:2619 之前做人脸识别的时候遇到一个问题,把RGB数据转换成Bitmap.所以决定写一个RGB和Bitmap互相转换的 ...

  6. Android App开发之位图加工Bitmap中转换位图的像素色彩、裁剪内部区域、利用矩阵变换位图的讲解及实战(附源码和演示)

    需要图片集和源码请点赞关注收藏后评论区留言~~~ 一.转换位图的像素色彩 给图片添加装饰物,只是在局部变换,如果想让图片一边保持轮廓一边改变色彩,就要深入图像的每个像素点,将这些像素点统统采取某种算法 ...

  7. 关于HBITMAP,CBITMAP,BITMAP的转换以及图像显示的一点归纳

    OK,在上一篇文章中我提到了VC6和VS的差别,在VC6中我们只能依赖于CBITMAP HBITMAP以及BITMAP之间的转化关系,而在VS中,我们用CIMAGE类就可以全部搞定,那么究竟这三个类之 ...

  8. (winform/Wpf字体转换)System.Drawing.FontFamily/System.Windows.Media.FontFamily互相转换

    winform下字体集: System.Drawing.FontFamily.Families wpf下字体集: System.Windows.Media.Fonts.SystemFontFamili ...

  9. 第52章,bitmap图像处理(从零开始学android),第52章、Bitmap图像处理(从零开始学Android)...

    1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitma ...

最新文章

  1. 吴裕雄--天生自然 物理学习与探索笔记:电功、电路以及欧姆定律
  2. 零点追踪(零点及量程补偿)
  3. 联发科梦碎高端化:2015年净利恐降4成股价腰斩
  4. Prism4翻译笔记(二)第二章:初始化Prism应用程序
  5. [JNI]开发之旅(5)访问c/c++函数
  6. UOS手动选择富士施乐打印机驱动
  7. android 积分墙广告平台有哪些,积分墙平台有哪些(全面解读积分墙)
  8. 浏览器 本地html 图片不显示,网页不显示图片怎么解决?
  9. LED MEO GTO GEO IGSO SSO概念
  10. html 怎么获取焦点的位置,jQuery怎么获取焦点?
  11. 余承东:华为技术走在产业前列,别人想超越很难;理想销量夺冠后,员工不满年终奖打折;黑客窃取GitHub代码签名证书|极客头条
  12. 期末安卓习题--第七章
  13. 【C语言】打印乘法口诀表
  14. linux软raid mdadm命令详讲
  15. Python 中 int 用法详解
  16. 面试了一个 46 岁的程序员,思绪万千
  17. HTML编写个人日记,HTML学习日记(1-基础)
  18. 炸了!就这个几Mb的开源插件,竟然把 Flash 复活了!安排的明明白白!
  19. Linux串口调试助手
  20. 基于PaddleDetection的行车检测

热门文章

  1. 菜鸟学习Spring——60s配置XML方法实现简单AOP
  2. POJ 2115 模线性方程 ax=b(mod n)
  3. ACM——A + B Problem (4)
  4. 【转】python 批量修改密码(paramiko)
  5. 找上海或深圳塑胶模具工作
  6. matlab生成exe失败,求助,m文件生成exe遇到的错误
  7. hive动态分区shell_Hive/Shell 创建Hive 库 ,表脚本,Hive 动态增加分区脚本
  8. android listview边框颜色,Android实现带有边框的ListView和item的方法
  9. java配置testng_如何使用TestNG JAVA Reflection设置测试方法执行的优先级
  10. Mysql 里的约束