Cut out a region of a bitmap

  • Thread starter VBTricks.de.vu Webmaster
  • Start date
V

VBTricks.de.vu Webmaster

Hello,

I have an bitmap in a resources file containing all icons displayed in
the toolbar. To display the icons I need to cut them out of the bitmap
and add them to an imagelist. The problem is the cutting out. I wrote
the following function:
[vb]
Public Shared Function CutOutIcon(ByRef vBitmap As Image, _
ByVal iIndex As Integer) As Image

Try
Dim Bmp2 As Bitmap = New Bitmap(32, 32)
Dim GFX As Graphics = Graphics.FromImage(Bmp2)
GFX.DrawImage(vBitmap, 0, 0, New Rectangle(iIndex * 32,_
0, 32, 32), GraphicsUnit.Pixel)
Return New Bitmap(32, 32, GFX)
Catch ex As Exception
End Try
End Function
[/vb]
vBitmap contains the pic from the resource (it really does, I checked
it). But all I get is a blank image.

What am I doing wrong?

Thanks in advance,
Stefan

--
___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> gmx <dot> net
_____________________________________________________
 
V

VBTricks.de.vu Webmaster

Misinterpreted the syntax of the Bitmap constructor. So replace line
Return New Bitmap(32, 32, GFX) by
Return Bmp2

Now I see something, but it's still not working correctly. The function
does not seem to cut out the right part. It cuts out only about 20x20
pixels and sizes it to 32x32. Must be something wrong with the dpi's...
Images 2 to 10 (iIndex 1 To 9) are empty as the first one before the
correction. The bitmap in the resources file has 75x75dpi.


Stefan

--
___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> gmx <dot> net
_____________________________________________________
 
T

Tom Spink

Hi Stefan,

Use the DrawImageUnscaled method (GFX.DrawImageUnscaled(...)), as opposed to
the DrawImage method, and this should solve your problem!

-- Tom Spink
 
V

VBTricks.de.vu Webmaster

This solved my problem:

Dim Bmp2 As Bitmap = New Bitmap(32, 32)
Dim GFX As Graphics = Graphics.FromImage(Bmp2)
Dim sourceRect As Rectangle = New Rectangle(count * 32, 0, 32, 32)
Dim destRect As Rectangle = New Rectangle(0, 0, 32, 32)
GFX.DrawImage(vbitmap, destRect, sourceRect, GraphicsUnit.Pixel)
Return Bmp2

Stefan

--
___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> gmx <dot> net
_____________________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top