Page 1 of 1

Problem returning bitmap

Posted: Sat Jul 31, 2010 8:44 pm
by bbanet
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

Posted: Mon Aug 02, 2010 3:34 pm
by bbanet
Nevermind. Seems to be related to some bad assembly references. Sorry.

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 3:46 pm
by Loïc
OK thank you for the update :)

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 3:52 pm
by bbanet
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.

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 3:56 pm
by bbanet
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

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 3:58 pm
by Loïc
I can't reproduce the issue.

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

Posted: Mon Aug 02, 2010 4:00 pm
by Loïc
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

Posted: Mon Aug 02, 2010 4:03 pm
by bbanet
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.

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 4:54 pm
by bbanet
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.

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 5:04 pm
by Loïc
OK. It is simply a bad GDI+ usage...

Please replace

Code: Select all

 Dim myBmp As Bitmap = bmp.Clone()

by

Code: Select all

Dim myBmp As New Bitmap(bmp)
This should work.

Re: Problem returning bitmap

Posted: Mon Aug 02, 2010 8:41 pm
by bbanet
Excellent! It worked. Thanks! :D