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. thinkPHP环境搭建小记

    php一直以来都被人诟病,说什么设计得很糟糕,有种你别用啊,不然就别bb了.最近,森哥在去年暑假学习了php基础和mvc模式的基础上准备用尝试一下国产ThinkPHP框架. 1.搭建LAMP环境 我实 ...

  2. Maven(二)使用eclipse创建maven多模块项目

    maven作为一种自动化构建工具,在现在的企业应用开发中运用非常普遍. 企业项目一般都比较大,多采用maven管理的多模块项目,下面直接上创建步骤 一.创建一个maven项目

  3. Linux 权限设置

    一.文件和目录权限 在Linux系统中,用户可以对每一个文件或目录都具有访问权限,这些访问权限决定了谁能访问,以及如何访问这些文件和目录. 1.文件权限简介 在Linux系统中,每一位用户都有对文件或 ...

  4. 造轮子之数据库对比工具DataBaseComparer

    最近同时在维护好几个项目,有些项目是SqlServer的,另一些是MySql的,DBA推荐了一个线上库和线下库的对比工具,用的时候经常会在对比时,半天都没有进度.索性自己这次造个轮子,做了一个纯对比数 ...

  5. crontab 提示 command not found 解决方案

    今天遇见一个问题,crontab的定时任务会报错:java command not found,但是手动执行脚本一直能成功. 猜想是环境变量的问题. 在crontab里添加个打印环境变量的任务: * ...

  6. Dev tdxDBTreeView

    可以快速的用tree展示层次结构,无需任何编码;对tree的操作会自动post到数据集:对数据集的操作会 在tree上表现 一.关键 设置 datasource displayField:节点的   ...

  7. caffe源码阅读(一)convert_imageset.cpp注释

    PS:本系列为本人初步学习caffe所记,由于理解尚浅,其中多有不足之处和错误之处,有待改正. 一.实现方法 首先,将文件名与它对应的标签用 std::pair 存储起来,其中first存储文件名,s ...

  8. 43、哈工大NLP自然语言处理,LTP4j的测试+还是测试

    1.首先需要构建自然语言处理的LTP的框架 (1)需要下载LTP的源码包即c++程序(https://github.com/HIT-SCIR/ltp)下载完解压缩之后的文件为ltp-master (2 ...

  9. 安装mysql 5.7+版本缺少data文件夹

    打开cmd命令窗口,并且进入到mysql安装目录的bin目录下.然后输入命令: mysqld --initialize-insecure --user=mysql 然后回车:去目录下查看,已经自动创建 ...

  10. gfw列表

    https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt