3 回答

TA贡献1842条经验 获得超12个赞
或者...试试这个 :)
public static ImageSource ToImageSource()
{
Bitmap bitmap = Properties.Resources.Image;
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
new Int32Rect(0, 0, bitmap.Width, bitmap.Height),
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
return wpfBitmap;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
调用DeleteObject将释放 GDI 句柄。

TA贡献1820条经验 获得超9个赞
遗憾的是我无法测试您的代码,但我认为您必须将模式设置为 NearestNeighbor
尝试这个
public static ImageSource ToImageSource()
{
Bitmap bitmap = Properties.Resources.Image;
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
RenderOptions.SetBitmapScalingMode(wpfBitmap, BitmapScalingMode.NearestNeighbor);
return wpfBitmap;
}

TA贡献1847条经验 获得超11个赞
尝试这个:
BitmapImage image = new BitmapImage(new Uri("your image path here", UriKind.Relative));
编辑:假设您有图像路径。
- 3 回答
- 0 关注
- 165 浏览
添加回答
举报