GDI+ Problems with LoadingPictures

02-17-2005, 04:50 AM

im trying around with the GDI+ Library and encountered some problems.
When i load a picture with the normal "LoadPicture" i am able to resize it in the same PictureBox.
If i load the same picture with gdi+ commands the resize does not work.

i attached a modified project to show the problem.
Just change the remarks in Form_Load to see the 2 possibilities and then start and press the left button.

Thanks in Advance

02-17-2005, 07:56 AM
You had several problems there. Mainly, you shouldn't call CreateFromHBitmap after LoadFromFile is called. Calling LoadFromFile created the GDI+ bitmap. In calling CreateFromHBitmap afterwards, you attempt to create a bitmap again, without Disposing the first.

Here is what you need:

Private Sub Form_Load()
Set bitmap = New cBitmap ' instantiate first, initializes GDI+
Set graphics = New cGraphics

' 1. Moeglichkeit
' Bild mit GDI+ laden
bitmap.LoadFromFile App.Path & "\" & "6605.gif"

' 2 Moeglichkeit
' Bild normal laden
' Set Picture1.Picture = LoadPicture(App.Path & "\" & "6605.gif")
' bitmap.CreateFromHBitmap Picture1.Picture.Handle

graphics.CreateFromHDC Picture1.hDC
End Sub

02-17-2005, 08:56 AM

thank you for your fast answer. It worked !
But now I tried to make two functions because of not having so much global variables.
One to load an image and one to resize it. Loading is working but resizing not.
I attached the project. It seems like "CreateFromHBitmap" does not work !
Can you find an error.

Where can i get detailed information about using your GDI+ ! I think i have a leak of principal understanding these functions. I already looked at microsoft SDK but that was not very helpful.

Any help appreciated

02-17-2005, 12:08 PM
One downside to creating your image from hBitmap (.Image.Handle) is that any alpha channel information is lost. This is not a problem if the image has no alpha, but I thought I should mention it.

Most of the time you'll want to use DrawImageRectI when drawing. DrawImageI amounts to drawing an image with whatever scale (dpi) is has internally, while drawing unscaled is fast, it also makes for unexpected image sizes, at times. I suggest always doing this:

graphics.DrawImageRectI bitmap.handle, 0, 0, bitmap.Width, bitmap.Height

Here is the fix for your ResizeImage procedure:

'bitmap.CreateFromHBitmap Bild.Picture.Handle
bitmap.CreateFromHBitmap Bild.Image.Handle

.Picture.Handle was 0

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum