By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

Cut out a region of a bitmap

P: n/a
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
__________________________________________________ ___
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
__________________________________________________ ___
Nov 21 '05 #2

P: n/a
put this outside orf trap
\\
end try
Return New Bitmap(32, 32, GFX)
end sub

regards

VBTricks.de.vu Webmaster wrote:
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

Nov 21 '05 #3

P: n/a
Hi Stefan,

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

-- Tom Spink

VBTricks.de.vu Webmaster wrote:
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

Nov 21 '05 #4

P: n/a
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
__________________________________________________ ___
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.