2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WPFの画像相互コンバーター。System.Drawing.BitmapからSystem.Windows.Controls.Imageへの変換。

Posted at

WPFで画像を扱おうとしたら、とても種類が多いことがわかったので、相互コンバーターを作ることにした。
まず最初は定番のSystem.Drawing.BitmapからSystem.Windows.Controls.Imageへの変換。

	[System.Runtime.InteropServices.DllImport("gdi32.dll")]
	public static extern bool DeleteObject(IntPtr hObject);
	public static System.Windows.Controls.Image
		SystemDrawingBitmap2SystemWindowsControlsImage(System.Drawing.Bitmap original) {

		IntPtr hBitmap = original.GetHbitmap();
		var newimage = new System.Windows.Controls.Image();
		try {
			newimage.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
				hBitmap,
				IntPtr.Zero,
				Int32Rect.Empty,
				System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
		}
		finally {
			DeleteObject(hBitmap);
		}
		return newimage;
	}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?