Problem returning bitmap
Problem returning bitmap
I am using GetBitmapFromGdPictureImage which is working, but when I try to return the Bitmap object to the calling assembly, nothing it returned. It seems like the Bitmap getting generated is in protected memory somehow and I cant get to it outside of the calling method. I have tried cloning the Bitmap and that has no impact. Am I missing something?
Re: Problem returning bitmap
Nevermind. Seems to be related to some bad assembly references. Sorry.
Re: Problem returning bitmap
OK thank you for the update
Re: Problem returning bitmap
OK - Maybe I spoke too soon. Here is my scenario:
I am wrapping the GdPicture code in my own dll. I have a copy page method that returns a bitmap. It is passed a tiff, select a specified page, calls GetBitmapFromGdPictureImage and returns a clone of that Bitmap.
This works, but when I call Bitmap.Save on the returned bitmap to save it to a file, I get a Protected Memory error.
I am wrapping the GdPicture code in my own dll. I have a copy page method that returns a bitmap. It is passed a tiff, select a specified page, calls GetBitmapFromGdPictureImage and returns a clone of that Bitmap.
This works, but when I call Bitmap.Save on the returned bitmap to save it to a file, I get a Protected Memory error.
Re: Problem returning bitmap
Here is the stack trace:
at System.Drawing.SafeNativeMethods.Gdip.GdipSaveImageToFile(HandleRef image, String filename, Guid& classId, HandleRef encoderParams)
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ConsoleApplication1.Program.Main(String[] args) in C:\Data\Sandbox\Square9.Imaging\ConsoleApplication1\Program.cs:line 52
at System.Drawing.SafeNativeMethods.Gdip.GdipSaveImageToFile(HandleRef image, String filename, Guid& classId, HandleRef encoderParams)
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ConsoleApplication1.Program.Main(String[] args) in C:\Data\Sandbox\Square9.Imaging\ConsoleApplication1\Program.cs:line 52
Re: Problem returning bitmap
I can't reproduce the issue.
Here what I tried:
Here what I tried:
Code: Select all
Dim ImageID As Integer = Imaging1.CreateGdPictureImageFromHwnd(Imaging1.GetDesktopHwnd)
Dim MyBitmap As Bitmap = Imaging1.GetBitmapFromGdPictureImage(ImageID)
MyBitmap.Save("c:\test.bmp")
Re: Problem returning bitmap
Also this code snippet works well:
Code: Select all
Dim ImageID As Integer = Imaging1.CreateGdPictureImageFromFile("multipage.tif")
Imaging1.TiffSelectPage(ImageID, 4)
Dim MyBitmap As Bitmap = Imaging1.GetBitmapFromGdPictureImage(ImageID)
MyBitmap.Save("c:\test.bmp")
Re: Problem returning bitmap
Your doing it all within the same project. Like I said, I am wrapping all of the GDPicture code in it's own DLL, so I have two projects in the solution. Project 1 is the DLL that has a public method to return a BMP. Project two is an EXE that calls into the DLL to get the BMP and then tries to save it to the file system with Bitmap.Save().
It works for me in the same project too.
It works for me in the same project too.
Re: Problem returning bitmap
It gets stranger...
Maybe I am just missing a project level setting or something, but I completely broke apart the code in question into a new project and did some testing. What I found is that if I call from a Windows Forms project it works, but if I call from a console app I get the protected memory error. I attached the project. You will need to recompile the projects in this solution. The C# console project will fail. The c# Winforms app will work. Same code.
Maybe I am just missing a project level setting or something, but I completely broke apart the code in question into a new project and did some testing. What I found is that if I call from a Windows Forms project it works, but if I call from a console app I get the protected memory error. I attached the project. You will need to recompile the projects in this solution. The C# console project will fail. The c# Winforms app will work. Same code.
- Attachments
-
- GdCrossProjectTest.zip
- (474.61 KiB) Downloaded 458 times
Re: Problem returning bitmap
OK. It is simply a bad GDI+ usage...
Please replace
by
This should work.
Please replace
Code: Select all
Dim myBmp As Bitmap = bmp.Clone()
by
Code: Select all
Dim myBmp As New Bitmap(bmp)
Re: Problem returning bitmap
Excellent! It worked. Thanks!
Who is online
Users browsing this forum: No registered users and 1 guest