public BitmapImage BitmapToImage(System.Drawing.Bitmap bitmap)
{
System.Drawing.Bitmap bitmapSource = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height);
int i, j;
for (i = 0; i < bitmap.Width; i++)
for (j = 0; j < bitmap.Height; j++)
{
System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
bitmapSource.SetPixel(i, j, newColor);
}
MemoryStream ms = new MemoryStream();
bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
bitmapImage.EndInit();

return bitmapImage;
}

public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
{
BitmapSource returnSource;
try
{
returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;
}
return returnSource;
}

public System.Drawing.Bitmap WpfBitmapSourceToBitmap(BitmapSource s)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(s.PixelWidth, s.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);
s.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
return bmp;
}

随机推荐

  1. AE开发示例之GPBufferLayer

    using System; using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime ...

  2. C# 访问https 未能创建 SSL/TLS 安全通道

    C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 一般GetResponse可以直接访问https ...

  3. android上下文菜单

    XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmln ...

  4. 转@ManyToMany- annotation关系映射篇(下)

    原文:http://blog.sina.com.cn/s/blog_6fef491d0100obdd.html 终于要说ManyToMany了 场景:Product和Customer. 先看TestP ...

  5. 20条Linux命令面试问答

    程序师  http://www.techug.com/20-linux-command-interview-questions 问:1 如何查看当前的Linux服务器的运行级别? 答: ‘who -r ...

  6. Word自动生成目录

    博主最近在写报告的时候要在Word里面做个目录,再做个页码,然后上网搜了一些方法,非常零散,我弄了好久才弄好.在这里我把整套方法分享一下. 声明:内容完全独创! 工具:Word 2016. 效果:如下 ...

  7. switch能使用的数据类型有6种

    byte.short.char.int.String.枚举

  8. html显示时间

    <html> <head> <script type="text/javascript"> function time() { var time ...

  9. thinkPHP-空操作

    空操作 当访问的方法不存在时,可以定义一个empty方法来避免空操作 function _empty() { echo "网页不存在,请检查地址信息"; } 这样当访问不存在的方法 ...

  10. 锋利的js前端分页之jQuery

    大家在作分页时,多数是在后台返回一个导航条的html字符串,其实在前端用js也很好实现. 调用pager方法,输入参数,会返回一个导航条的html字符串.方法的内部比较简单. /** * pageSi ...