3 回答

TA貢獻1842條經驗 獲得超13個贊
或者...試試這個 :)
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 關注
- 193 瀏覽
添加回答
舉報