Problem returning bitmap

General discussions about GdPicture.NET.
Post Reply
bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Problem returning bitmap

Post by bbanet » Sat Jul 31, 2010 8:44 pm

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?

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 3:34 pm

Nevermind. Seems to be related to some bad assembly references. Sorry.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Problem returning bitmap

Post by Loïc » Mon Aug 02, 2010 3:46 pm

OK thank you for the update :)

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 3:52 pm

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.

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 3:56 pm

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

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Problem returning bitmap

Post by Loïc » Mon Aug 02, 2010 3:58 pm

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")

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Problem returning bitmap

Post by Loïc » Mon Aug 02, 2010 4:00 pm

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")

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 4:03 pm

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.

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 4:54 pm

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.
Attachments
GdCrossProjectTest.zip
(474.61 KiB) Downloaded 458 times

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Problem returning bitmap

Post by Loïc » Mon Aug 02, 2010 5:04 pm

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.

bbanet
Posts: 17
Joined: Mon Jun 15, 2009 11:03 pm

Re: Problem returning bitmap

Post by bbanet » Mon Aug 02, 2010 8:41 pm

Excellent! It worked. Thanks! :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest