In my WPF application images are taken with a camera use a view and then passed as bitmaps to another when it's closed via eventargs. However, when I then try to process the images I get the AccessViolationException. This does not occure when I process the images before they are passed or when I use images loaded from a file.
Getting the image from the camera (The PtCamera class is my wrapper for the Camera class from the API)
Bitmap GetRefImage(PtCamera cam)
{
Bitmap image = new Bitmap(2560, 1920, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
if (cam.IsConnected)
{
cam.FetchImage(out image);
}
else
{
ErrorOccurred?.Invoke(this, $"GetRefImage: {cam.Error}");
}
return image;
}
In this context I can access the bitmap and process it as I like.
After passing the bitmap when the view is closed:
void CloseZoomedView(bool isConf)
{
if (cam is object && cam.IsConnected)
cam.Close();
ZoomClosingArgs eArg = new ZoomClosingArgs()
{
IsConfirmed = isConf,
RefImage = refImage,
};
ClosingZoom?.Invoke(this, eArg);
}
The exception occurs directly when accessing the data in the other viewmodel:
void HandleZoomImageClosed(object sender, ZoomClosingArgs e)
{
if (e is object && e.IsConfirmed)
{
Color test = e.RefImage.GetPixel(0, 0);
//...
}
}
The bitmap is generated by accessing the memory of the camera via FetchImage()
public void FetchImage(out Bitmap image)
{
camera.Memory.GetActive(out int memID);
camera.Memory.ToBitmap(memID, out image);
}
If i replace the code in FetchImage() with just a new Bitmap from file
image = new Bitmap(#"d:\testimage.png")
It works without problems in any context.
The API documentation simply states the following:
Accessible
Camera.Memory.ToBitmap
Syntax
uEye.Memory.ToBitmap(int s32MemId, out System.Drawing.Bitmap bitmap)
Description
Returns a bitmap which contains the image. The method uses the already
allocated image memory and the image is displayed in the format you
specified when allocating the image memory.
Any hints are much appreciated.
I was closing my camera object too early. In CloseZoomedView() the cam.Close() method releases all the memory areas taken up by the camera. With passing a new Bitmap before closing it works like a charm.
void CloseZoomedView(bool isConf)
{
ZoomClosingArgs eArg = new ZoomClosingArgs()
{
IsConfirmed = isConf,
RefImage = new Bitmap(refImage),
};
if (cam is object && cam.IsConnected)
cam.Close();
ClosingZoom?.Invoke(this, eArg);
}
Related
I'm a newby in C#. I have to repeatedly refresh a GUI picture box in a worker thread. The image is acquired from a camera polling a driver with a GetImage method that retrives the image to be displayed. Even if I allocate the bitmap using directive "using" and explicitly call G.C, memory seems to be never deallocated.
The worker thread is something like this:
while (true)
{
// request image with IR signal values (array of UInt16)
image = axLVCam.GetImage(0);
lut = axLVCam.GetLUT(1);
DrawPicture(image, lut);
//GC.Collect();
}
While the DrawPicture method is something like
public void DrawPicture(object image, object lut)
{
[...]
// We have an image - cast it to proper type
System.UInt16[,] im = image as System.UInt16[,];
float[] lutTempConversion = lut as float[];
int lngWidthIrImage = im.GetLength(0);
int lngHeightIrImage = im.GetLength(1);
using (Bitmap bmp = new Bitmap(lngWidthIrImage, lngHeightIrImage)) {
[...many operation on bitmap pixel...]
// Bitmap is ready - update image control
//SetControlPropertyThreadSafe(tempTxtBox, "Text", string.Format("{0:0.#}", lutTempConversion[im[160, 100]]));
//tempTxtBox.Text = string.Format("{0:00000}", im[160, 100]);
//System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
}
}
Problems arises with the
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
In fact commenting that line of code, garbage collection works as it would.
Better, the problem seems to be with
System.Drawing.Image.FromHbitmap(bmp.GetHbitmap())
Any advice to solve this memory leak?
Thanks a lot!
Image implements IDisposable, so you should call Dispose on each Image instance that you create, when it is no longer needed. You could try to replace this line in your code:
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
With this:
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
This will dispose the previous image (if any) before the new one is assigned.
The thing is, you're making a GDI bitmap of bmp with GetHbitmap, which according to msdn:
You are responsible for calling the
GDI DeleteObject method to free the
memory used by the GDI bitmap object.
Then the FromHbitmap method makes a copy of the GDI bitmap; so you can release the incoming GDI bitmap using the GDI DeleteObject method immediately after creating the new Image.
So basically I would add:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
...
IntPtr gdiBitmap = bmp.GetHbitmap();
// Release the copied GDI bitmap
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = System.Drawing.Image.FromHbitmap(gdiBitmap);
// Release the current GDI bitmap
DeleteObject(gdiBitmap);
I am unsure if you need the GDI bitmap to perform some kind of transformation. In case you don't you can just assign the bitmap to the Image property of your PictureBox, and ignore the former solution:
// Since we're not using unmanaged resources anymore, explicitly disposing
// the Image only results in more immediate garbage collection, there wouldn't
// actually be a memory leak if you forget to dispose.
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = bmp;
There are several ways to release an image from pbox. I strongly recommend the way is that do not use pbox.Image = Image.FromFile.... If you do not use FileStream and you want to read it from file use BitMap class. Like this:
Bitmap bmp = new Bitmap(fileName);
pbox.Image = bmp; // notice that we used bitmap class to initialize pbox.
... and then you want to release the image file bmp.Dispose();
Now you can delete, move or whatever you want to the file.
I have Xamarin Android app.
I try to save Linear layout like Bitmap . Here is code
public void Save()
{
LinearLayout view = FindViewById<LinearLayout>(Resource.Id.badge);
view.DrawingCacheEnabled = true;
view.BuildDrawingCache();
Bitmap layout = view.GetDrawingCache(true);
}
I need to save it to Pictures folder. How I can do this?
He explained the way to save bitmap as png in memory card with c #. I hope to understand your problem is correct.
"This here is a slim way to export a Bitmap as PNG-file to the sd-card using only C# stuff"
https://stackoverflow.com/a/29012075/6322661
You can use Canvas to draw a View by the following code:
public Bitmap createViewBitmap(View v)
{
Bitmap bitmap = Bitmap.CreateBitmap(v.Width, v.Height,
Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
v.Draw(canvas);
return bitmap;
}
Linear layout is a kind of View. So you can create a Linear layout BitMap :
View v = FindViewById<LinearLayout>(Resource.Id.myLinearLayout);
Bitmap myBitMap = createViewBitmap(v);
And then save it in the DCIM folder:
public static void saveImage(Bitmap bmp)
{
try
{
using (var os = new System.IO.FileStream(Android.OS.Environment.ExternalStorageDirectory + "/DCIM/Camera/MikeBitMap2.jpg", System.IO.FileMode.CreateNew))
{
bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
}
}
catch (Exception e)
{
}
}
You can refer to my github for the more code information.
Im in the process of making a snipping tool. I've got it so that my program can be used to draw a rectangle with the mouse and have that image saved. Now what I'm trying to do is have the image generated be transfered to a picture box that show's the user what they have just captured before they decide to save it or anything else.
Is there a way in which I can do this?
So far my screen capture code saves the captured image to the clipboard with the following code found in my ScreenCapture class:
public static bool saveToClipboard = true;
public static void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
{
using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
if (showCursor)
{
Rectangle cursorBounds = new Rectangle(curPos, curSize);
Cursors.Default.Draw(g, cursorBounds);
}
}
if (saveToClipboard)
{
Image img = (Image)bitmap;
Clipboard.SetImage(img);
}
}
}
Has anyone ever done something like this before? Also, is it possible to have the picture box auto resize so that the screen capture size is used and not the picture boxes?
update
Further to some of the comments below, I've been trying to save the image I store in my above class and pass it to the picture box. But nothing is displayed when I do it. The code I've been using is thus:
Held on the form1.cs:
public void SetImage(Image img)
{
imagePreview.Image = img;
}
And in the screen capture class:
if (saveToClipboard)
{
Image img = (Image)bitmap;
ControlPanel cp = new ControlPanel();
cp.SetImage(img);
Clipboard.SetImage(img);
}
ControlPanel cp = new ControlPanel();
cp.SetImage(img);
this won't work because you need to access the parent form in use, not create a new instance of it.
Look at the answer to this question on creating a simple custom event, but add an Image to the ProgressEventArgs that they create. Then on your main form, subsribe to the event and update the picturebox from there.
I'm a newby in C#. I have to repeatedly refresh a GUI picture box in a worker thread. The image is acquired from a camera polling a driver with a GetImage method that retrives the image to be displayed. Even if I allocate the bitmap using directive "using" and explicitly call G.C, memory seems to be never deallocated.
The worker thread is something like this:
while (true)
{
// request image with IR signal values (array of UInt16)
image = axLVCam.GetImage(0);
lut = axLVCam.GetLUT(1);
DrawPicture(image, lut);
//GC.Collect();
}
While the DrawPicture method is something like
public void DrawPicture(object image, object lut)
{
[...]
// We have an image - cast it to proper type
System.UInt16[,] im = image as System.UInt16[,];
float[] lutTempConversion = lut as float[];
int lngWidthIrImage = im.GetLength(0);
int lngHeightIrImage = im.GetLength(1);
using (Bitmap bmp = new Bitmap(lngWidthIrImage, lngHeightIrImage)) {
[...many operation on bitmap pixel...]
// Bitmap is ready - update image control
//SetControlPropertyThreadSafe(tempTxtBox, "Text", string.Format("{0:0.#}", lutTempConversion[im[160, 100]]));
//tempTxtBox.Text = string.Format("{0:00000}", im[160, 100]);
//System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
}
}
Problems arises with the
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
In fact commenting that line of code, garbage collection works as it would.
Better, the problem seems to be with
System.Drawing.Image.FromHbitmap(bmp.GetHbitmap())
Any advice to solve this memory leak?
Thanks a lot!
Image implements IDisposable, so you should call Dispose on each Image instance that you create, when it is no longer needed. You could try to replace this line in your code:
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
With this:
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
This will dispose the previous image (if any) before the new one is assigned.
The thing is, you're making a GDI bitmap of bmp with GetHbitmap, which according to msdn:
You are responsible for calling the
GDI DeleteObject method to free the
memory used by the GDI bitmap object.
Then the FromHbitmap method makes a copy of the GDI bitmap; so you can release the incoming GDI bitmap using the GDI DeleteObject method immediately after creating the new Image.
So basically I would add:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
...
IntPtr gdiBitmap = bmp.GetHbitmap();
// Release the copied GDI bitmap
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = System.Drawing.Image.FromHbitmap(gdiBitmap);
// Release the current GDI bitmap
DeleteObject(gdiBitmap);
I am unsure if you need the GDI bitmap to perform some kind of transformation. In case you don't you can just assign the bitmap to the Image property of your PictureBox, and ignore the former solution:
// Since we're not using unmanaged resources anymore, explicitly disposing
// the Image only results in more immediate garbage collection, there wouldn't
// actually be a memory leak if you forget to dispose.
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = bmp;
There are several ways to release an image from pbox. I strongly recommend the way is that do not use pbox.Image = Image.FromFile.... If you do not use FileStream and you want to read it from file use BitMap class. Like this:
Bitmap bmp = new Bitmap(fileName);
pbox.Image = bmp; // notice that we used bitmap class to initialize pbox.
... and then you want to release the image file bmp.Dispose();
Now you can delete, move or whatever you want to the file.
I have a windows form where I set the BackgroundImage property to a custom bitmap image.
private Image MakeCustomBackground()
{
Bitmap result = new Bitmap(100, 100);
using(Graphics canvas = Graphics.FromImage(result))
{
// draw the custom image
}
return result;
}
private void UpdateFromBackground()
{
this.BackgroundImage = MakeCustomBackground();
}
My question is, Image is disposable and I am creating it, does that mean that I must dispose of it? Or when I pass the image to the form, via BackgroundImage, does it take ownership and dispose of it when it no longer needs it?
Assuming that UpdateFromBackground() is called more than once, you probably should Dispose the old Image when (before) setting a new one. If you don't then the GC will do it eventually but that is less efficient. The Form will only Dispose the last BgImage you assigned.
private void UpdateFromBackground()
{
if (this.BackgroundImage != null)
{
this.BackgroundImage.Dispose();
}
this.BackgroundImage = MakeCustomBackground();
}