WPF - Update "System.Windows.Controls.Image" from another thread - c#

I get this exception on that code.
How to fix it?
Excepton:
The calling thread cannot access this
object because a different thread owns
it.
Code:
void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
{
IntPtr hBitMap = image.GetHbitmap();
BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
Dispatcher.BeginInvoke((Action)(() =>
{
labelX.Content = String.Format("X: {0}", Center.X); //OK Working
labelY.Content = String.Format("Y: {0}", Center.Y); //OK Working
pictureBoxMain.Source = bmaps; // THERE IS EXCEPTON
}), DispatcherPriority.Render, null);
}
pictureBoxMain is System.Windows.Controls.Image.

You can freeze the BitmapSource so that it can be accessed from any thread:
void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
{
IntPtr hBitMap = image.GetHbitmap();
BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
bmaps.Freeze();
Dispatcher.BeginInvoke((Action)(() =>
{
labelX.Content = String.Format("X: {0}", Center.X);
labelY.Content = String.Format("Y: {0}", Center.Y);
pictureBoxMain.Source = bmaps;
}), DispatcherPriority.Render, null);
}

You could Freeze the image, as suggested in another thread, which gets rid of the threading restriction but makes the image immutable.
WPF/BackgroundWorker and BitmapSource problem

Related

Convert ImageSource to Bitmap [duplicate]

As far as I can tell the only way to convert from BitmapSource to Bitmap is through unsafe code... Like this (from Lesters WPF blog):
myBitmapSource.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;
}
}
To do the reverse:
System.Windows.Media.Imaging.BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
Is there an easier way in the framework? And what is the reason it isn't in there (if it's not)? I would think it's fairly usable.
The reason I need it is because I use AForge to do certain image operations in an WPF app. WPF wants to show BitmapSource/ImageSource but AForge works on Bitmaps.
It is possible to do without using unsafe code by using Bitmap.LockBits and copy the pixels from the BitmapSource straight to the Bitmap
Bitmap GetBitmap(BitmapSource source) {
Bitmap bmp = new Bitmap(
source.PixelWidth,
source.PixelHeight,
PixelFormat.Format32bppPArgb);
BitmapData data = bmp.LockBits(
new Rectangle(Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppPArgb);
source.CopyPixels(
Int32Rect.Empty,
data.Scan0,
data.Height * data.Stride,
data.Stride);
bmp.UnlockBits(data);
return bmp;
}
You can just use these two methods:
public static BitmapSource ConvertBitmap(Bitmap source)
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
source.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
Bitmap bitmap;
using (var outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}
It works perfectly for me.
Is this what your looking for?
Bitmap bmp = System.Drawing.Image.FromHbitmap(pBits);
Here a code to set transparent background to any bitmap resource within a Resource Dictionary (not Resources.resx often used in Windows.Forms age). I call this methode before InitializeComponent() - methode. The methodes 'ConvertBitmap(Bitmap source)' and BitmapFromSource(BitmapSource bitmapsource) are mentioned in post from melvas above.
private void SetBitmapResourcesTransparent()
{
Image img;
BitmapSource bmpSource;
System.Drawing.Bitmap bmp;
foreach (ResourceDictionary resdict in Application.Current.Resources.MergedDictionaries)
{
foreach (DictionaryEntry dictEntry in resdict)
{
// search for bitmap resource
if ((img = dictEntry.Value as Image) is Image
&& (bmpSource = img.Source as BitmapSource) is BitmapSource
&& (bmp = BitmapFromSource(bmpSource)) != null)
{
// make bitmap transparent and assign it back to ressource
bmp.MakeTransparent(System.Drawing.Color.Magenta);
bmpSource = ConvertBitmap(bmp);
img.Source = bmpSource;
}
}
}
}
This is neat and faster than light:
return Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero,
Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() );
You can share the pixeldata between both namespaces. You don't have to convert.
Use the SharedBitmapSource. https://stackoverflow.com/a/32841840/690656

WPF streaming Bitmap data into an Image control across threads

I have a WPF application, and am streaming a camera frame from an opencv DLL into an Image control inside a UserControl.
This works for a while, and then crashes, giving me:
The calling thread cannot access this object because a different thread owns it.
My code is as follows:
The class that calls the image: (running in a thread)
private void imageShow()
{
while (true)
{
if (status == 1)
{
IntPtr ptr = getFrame(); // The DLL function that returns the image.
imgHalfSize = new Bitmap(640, 360, 3 * 640, PixelFormat.Format24bppRgb, ptr);
CameraFrame = ToBitmapSource(imgHalfSize);
CameraFrame.Freeze();
Thread.Sleep(20);
}
}
//conversion from Bitmap to BitmapSource
public BitmapSource CameraFrame;
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public static BitmapSource ToBitmapSource(Bitmap source)
{
IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr); //release the HBitmap
return bs;
}
//The userControl that displays the image:
//xaml
<Image Name="ImageCameraFrame"/>
cs:
public FormCameraViewFull(DllFunctions _Dense)
{
Dense = _Dense; // The class as above
InitializeComponent();
Task.Factory.StartNew(() =>
{
InvokeMethodExample();
});
}
//thread function:
private void InvokeMethodExample()
{
while (true)
{
ImageCameraFrame.Dispatcher.Invoke((Action)(() => ImageCameraFrame.Source = Dense.CameraFrame));
}
}
As above, this works great for some time, then crashes. I was under the impression that the line CameraFrame.Freeze(); should stop this behavior, but I am clearly doing something wrong. Do I need a mutex, or similar lock here?
Thank you.
Use a DispatcherTimer with (an optionally async) Tick handler:
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(50)
};
timer.Tick += async (s, e) =>
{
ImageCameraFrame.Source = await Task.Run(() =>
{
var data = Dense.getFrame();
var format = PixelFormats.Rgb24;
var width = 640;
var height = 360;
var stride = (width * format.BitsPerPixel + 7) / 8;
var bitmap = BitmapSource.Create(width, height, 96, 96,
format, null, data, stride * height, stride);
bitmap.Freeze();
return bitmap;
});
};
timer.Start();

OutOfMemory exception with BitmapSource in WPF app

Task: I got 2 monitors. And I need to show on #1 what is going on #2. In another words, first monitor is nothing but a reflector of second.
Current solution: Just making screenshot every ~100ms and re-render.
Following method is responsible for capturing screenshots:
private BitmapSource MakeScreenshot(Screen screen)
{
using (var screenBmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size);
return
Imaging.CreateBitmapSourceFromHBitmap(
screenBmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
}
After that I uset Start(...) method to run my "reflection" from second screen to first:
public void Start(int delay, int period)
{
if (_timer != null) throw new InvalidOperationException();
_timer = new System.Threading.Timer(
_ =>
{
_placeholder
.Dispatcher
.Invoke(() =>
{
_placeholder.Source = MakeScreenshot(_targetScreen); // re-render new screenshot
});
},
null,
delay,
period);
}
Problem: After around 30-40 second of pretty nice run it fails with OutOfMemoryException. I've investigated some of posts here, but found nothing regarding my problem.
That is because you leak memory here:
Imaging.CreateBitmapSourceFromHBitmap(
screenBmp.GetHbitmap(), // < here
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
You need to free memory used by GDI bitmap after you call screenBmp.GetHbitmap(). Change that like this:
private BitmapSource MakeScreenshot(Screen screen)
{
using (var screenBmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size);
var handle = screenBmp.GetHbitmap();
try {
return
Imaging.CreateBitmapSourceFromHBitmap(
handle,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally {
DeleteObject(handle);
}
}
}
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
And it should not leak any more.

Display BitmapSource in WPF not working

The following code takes a snapshot of part of the screen (at the mouse coordinates) and should display it in an Image control.
public partial class MainWindow : Window
{
Timer timer = new Timer(100);
public MainWindow()
{
InitializeComponent();
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
viewerImage.Source = GetSnapAtMouseCoords((int)((Grid)viewerImage.Parent).ActualWidth, (int)((Grid)viewerImage.Parent).ActualHeight, System.Windows.Forms.Cursor.Position);
}
private BitmapSource GetSnapAtMouseCoords(int width, int height, System.Drawing.Point mousePosition)
{
IntPtr handle = IntPtr.Zero;
try
{
using (var screenBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(mousePosition.X, mousePosition.Y, 0, 0, screenBmp.Size);
handle = screenBmp.GetHbitmap();
var bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
handle,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return bs;
}
}
}
finally
{
DeleteObject(handle);
}
}
}
Everything works up to the point where I set the image source to the BitmapSource. Unfortunately, the image is never rendered on screen.
I think that maybe this is because I am creating the BitmapSource on the GUI thread... But I am not so sure.
Any suggestions or ideas welcome.
Actually it's because you are accessing your GUI on a different Thread. You can either wrap the initial call like this:
Dispatcher.BeginInvoke(new Action(() =>
{
viewerImage.Source = GetSnapAtMouseCoords(
(int)((Grid)viewerImage.Parent).ActualWidth,
(int)((Grid)viewerImage.Parent).ActualHeight,
System.Windows.Forms.Cursor.Position);
}));
Or do all the processing in a background thread an just return a Frozen (Thread safe) BitmapSource. You would hover need to pass (int)((Grid)viewerImage.Parent).ActualWidth differently, since that is owned by the UI thread too.
bs.Freeze();
Dispatcher.BeginInvoke(new Action(() =>
{
viewerImage.Source = bs;
}));

Grab Bitmap-Image (Winforms) and convert to WPF-Image fails

I grab an image from a external camera. The winforms application works so far. Now I need to transfer it to WPF. Therefore I converted the bitmap-image to WPF like this:
GrabHandler frameCallbackDelegate;
void frameCallBack(IntPtr lpUserData, ref VCECLB_FrameInfoEx frameInfo)
{
if (frameInfo.dma_status != 0) return;
// fill image
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmdata = image.LockBits(rect, ImageLockMode.ReadWrite, image.PixelFormat);
UInt32 outputBitDepth;
IntPtr stride = new IntPtr(bmdata.Stride);
VCECLB_Error err = NativeFunctions.VCECLB_UnpackRawPixelsEx(ref pRawPixelInfo, frameInfo.lpRawBuffer, bmdata.Scan0, ref stride, VCECLB_Output_Format.VCECLB_EX_FMT_TopDown | VCECLB_Output_Format.VCECLB_EX_FMT_3Channel, out outputBitDepth);
image.UnlockBits(bmdata);
Dispatcher.Invoke(new Action(() =>
{
// Convert bitmap to WPF-Image
var bmp = new Bitmap(image);
var hBitmap = bmp.GetHbitmap();
ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
image_LowLight.Source = wpfBitmap;
DeleteObject(hBitmap);
image.Dispose();
}));
GC.Collect();
}
private void StartGrab(object sender, RoutedEventArgs e)
{
start_button.IsEnabled = true;
stop_button.IsEnabled = false;
frameCallbackDelegate = new GrabHandler(frameCallBack);
m_ImageAquisition.StartGrab(frameCallbackDelegate);
}
When I enter for the first time the frameCallBack from the StartGrab, the image.Width and image.Height have a valid value (not 0) and it seems correct. When the frameCallBack is entered for the second time all image-data (width and height) are 0 and therefore rect gives an error.
Is there something wrong in the way I am using the Dispatcher.Invoke to get a valid bitmap-image?
Thanks!
Avoid using volatile data in an Action() which gets executed some time later by the Dispatcher.
Refactor your code like this:
GrabHandler frameCallbackDelegate;
void frameCallBack(IntPtr lpUserData, ref VCECLB_FrameInfoEx frameInfo)
{
if (frameInfo.dma_status != 0) return;
// fill image
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmdata = image.LockBits(rect, ImageLockMode.ReadWrite, image.PixelFormat);
UInt32 outputBitDepth;
IntPtr stride = new IntPtr(bmdata.Stride);
VCECLB_Error err = NativeFunctions.VCECLB_UnpackRawPixelsEx(ref pRawPixelInfo, frameInfo.lpRawBuffer, bmdata.Scan0, ref stride, VCECLB_Output_Format.VCECLB_EX_FMT_TopDown | VCECLB_Output_Format.VCECLB_EX_FMT_3Channel, out outputBitDepth);
image.UnlockBits(bmdata);
// Convert bitmap to WPF-Image
var bmp = new Bitmap(image);
var hBitmap = bmp.GetHbitmap();
ImageSource wpfBitmap =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
image.Dispose();
Dispatcher.Invoke(new Action(() =>
{
image_LowLight.Source = wpfBitmap;
}));
GC.Collect();
}

Categories

Resources