Can't get on with this image lark!

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
Any chance somebody can enlighten me as to where Im going wrong please, as this code does not error but does not draw the image on the button either

Code:
 get the image to resize

Dim imgImage As Image = Image.FromFile("c:\anyjpg.jpg")

 create a size object

Dim szNewImageSize As New System.Drawing.Size()

 set the new size

szNewImageSize.Height = 100
szNewImageSize.Width = 100

 load the resized image into bmpBitMap

Dim bmpBitMap As New Bitmap(imgImage, szNewImageSize)

 create a graphics object from Button1

Dim gfxButton1 As Graphics = Me.Button1.CreateGraphics()

 try to draw the image on the button

gfxButton1.DrawImage(bmpBitMap, 0, 0)
:confused:
 
Can you set the buttons Image property (and ImageAlign)? Seems easier, unless you need something fancy like more exact placement or something else.

Heres a oneliner to load and resize an image for a button:
Code:
Button1.Image = New Bitmap(New Bitmap("C:\anyjpg.jpg"), 32, 40)

Im not sure where you put your code, but if it were in Form_Load or similar, it wont work. Youd have to override the buttons paint event or maybe use the Forms paint event. Im not real familiar with trying either since Ive always been able to use the Image property of the button.

-Nerseus
 
Thanks Nerseus, Ill give it ago tonight and see what happens...
 
Nerseus, that is perfect....ta muchly. Now I obvioulsy want to work this out for myself so could you point me in the right direction whereby I can take a section of the source image and use this section as the button image. Like I say, dont tell me how to do it otherwise I wont learn ought, but a pointer would be welcome seeing as I was way off beam with this first attempt :-))
 
Result :-)

Code:
Dim bmpNewImage As Bitmap = New Bitmap(strImageFilePath)
Dim bmpImageSection As Bitmap

bmpImageSection = bmpNewImage.Clone(New Rectangle(0, 0, 100, 100), Drawing.Imaging.PixelFormat.DontCare)

Button1.Image = New Bitmap(bmpImageSection, 100, 100)

My only problem is this.

I wouldnt have worked this out without the section of code re clone being in one of my VB books. I spent ages searching the help within VB and got nowhere.

Any ideas how to refine the help search? All my results just kept bringing back help on modifying images in design mode using menu options and design tools.

I found noting on the above at all. I didnt know about clone so would never had homed in on it :-(
 
Mmm, now I find that it cannot open any image of gif, bmp or jpg. It appears to error if the source file is a photo, I get Out of memory error!

I see there are other formats other than DontCare, but how do I make it generic so it really doesnt care?
 
Use bmpNewImage.Format.

As for finding help, sometimes its useful to type your object and the "." then just scroll through the list of members to see whats available. Also, try the object class followed by a period to see the static methods. So, try:
bmpNewImage. (see instance members)
Bitmap. (see static members)

-Nerseus
 
Thanks for this Nerseus, Ill try this out re the finding help.

As for the Format method, there isnt one on bmpNewImage or Bitmap either?
 
Oh bugger I take it all back.

Consider this a good lesson from you both. I sussed this by using the object brower!!!

Oh will I ever learn..........hopefully :-)
 
Back
Top