Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting a Controls.Image Width and Height
    text
    copied!<p>I'm new to WPF and am trying to use a a Controls.Image. I have it set to stretch when the form expands. I am trying to generate a Drawing.Bitmap at runtime to display on the Controls.Image in WPF. When I generate the Drawing.Bitmap I only want to create one as big as the Image control on the screen is (don't waste cpu cycles). To create the image I have the following code. <code>Bitmap bImage = new Bitmap((int)imgInput.Width, (int)imgInput.Height);</code>imgInput is the Controls.Image and bImage obviously is the Drawing.Bitmap. However the imgInput.Width and Height are showing as 0 at runtime although they are definitely not at design time. If I don't try to dynamically stretch the image to expand with the window there is no issue it shows as expected. So how do I actually get the image. Also does anyone have a better way than creating a Drawing.Bitmap in memory and converting it over to a ImageSource for WPF? I am not loading an image from a URI or anything like that I have to create it in memory.</p> <p>I am binding the image by the following code</p> <pre><code> imgInput.Source = loadBitmap(bImage); } [DllImport("gdi32")] static extern int DeleteObject(IntPtr o); public static BitmapSource loadBitmap(System.Drawing.Bitmap source) { BitmapSource bs = null; IntPtr ip = source.GetHbitmap(); try { bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(ip); } return bs; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload