图片类型转换

longjun1627 2008-10-30 04:29:27
在WPF中想将其中的图片换到WINFORM中去用,老是出现下面的错误,各位碰到过吗?说点方法呀.

无法从System.Windows.Media.ImageBrush转换为System.Drawing.Image

无法从System.Windows.Media.Imaging.RenderTargetBitmap转换为System.Drawing.Image
...全文
1068 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nvshenxp 2011-12-12
  • 打赏
  • 举报
回复
楼主,请教你个问题,如何把Image转化成Bitmap。请帮下忙,谢谢!
longjun1627 2008-10-31
  • 打赏
  • 举报
回复
WPF问题,怎么这么少人关注呀?

longjun1627 2008-10-31
  • 打赏
  • 举报
回复
无语了,没人关注,自己解决了,帖出来共享下吧

public System.Drawing.Bitmap GetBitmap(BitmapSource image)
{
System.Windows.Media.Imaging.BitmapSource bitmapSource = image as BitmapSource;

double newWidthRatio = image.Width / (double)bitmapSource.PixelWidth;
double newHeightRatio = ((image.Width * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / (double)bitmapSource.PixelHeight;

System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap(
bitmapSource,
new System.Windows.Media.ScaleTransform(newWidthRatio, newHeightRatio));

int width = transformedBitmapSource.PixelWidth;
int height = transformedBitmapSource.PixelHeight;
int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8);

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

transformedBitmapSource.CopyPixels(bits, stride, 0);

unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);

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

return bitmap;
}
}
}
zlb789 2008-10-30
  • 打赏
  • 举报
回复
帮你顶
longjun1627 2008-10-30
  • 打赏
  • 举报
回复
楼上的能说的详细吗?给点源代码呀
多谢了
palmax 2008-10-30
  • 打赏
  • 举报
回复
用二进制流做中转

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧