Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Any chance somebody can enlighten me as to where I'm going wrong please, as this code does not error but does not draw the image on the button either

 


' 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:

My website
  • *Experts*
Posted

Can you set the button's Image property (and ImageAlign)? Seems easier, unless you need something fancy like more exact placement or something else.

 

Here's a oneliner to load and resize an image for a button:

Button1.Image = New Bitmap(New Bitmap("C:\anyjpg.jpg"), 32, 40)

 

I'm not sure where you put your code, but if it were in Form_Load or similar, it won't work. You'd have to override the button's paint event or maybe use the Form's paint event. I'm not real familiar with trying either since I've always been able to use the Image property of the button.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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, don't tell me how to do it otherwise I won't learn ought, but a pointer would be welcome seeing as I was way off beam with this first attempt :-))
My website
Posted

Result :-)

 


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 wouldn't 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 didn't know about clone so would never had homed in on it :-(

My website
Posted

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 doesn't care?

My website
  • *Experts*
Posted

Use bmpNewImage.Format.

 

As for finding help, sometimes it's useful to type your object and the "." then just scroll through the list of members to see what's 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks for this Nerseus, I'll try this out re the finding help.

 

As for the Format method, there isn't one on bmpNewImage or Bitmap either?

My website
Posted

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

My website

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...