473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Cut out a region of a bitmap

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
4 3322
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Wolff | last post by:
Hi !! To draw multiple graphics objects which have different shapes, I’m using the region class to exclude/union the drawing area of these Objects. The rectangle around a single object is of...
2
by: Brian Henry | last post by:
I can get an image into the MDI client region with the following code but it tiles, how can i make it center only? thanks Dim c As Control For Each c In Me.Controls If c.GetType.Name =...
2
by: Willie jan | last post by:
Hi, i try to create a bitmap with transparent regions, but it does not work. the transparent region turns out to be black. anybody some ideas?? Dim bmap As...
2
by: Mark Denardo | last post by:
Hi, I need some expert GDI+ person to help me with my RoundOffImage Function: What I'm trying to do is take in an image, crop off the edges around an ellipse region I set up, and then return the...
7
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
3
by: Manuel | last post by:
Hi to all, I have a System.Drawing.Graphics, I need to clip a region and put it in a different another Graphics with the size of clipped region. There's suggestion or a tutorial? Thanks in...
8
by: Joergen Bech | last post by:
Suppose I have Dim bm As New Bitmap(16, 16,Imaging.PixelFormat.Format8bppIndexed) I cannot use Dim g As Graphics = Graphics.FromImage(bmdest) Dim hdc As IntPtr = g.GetHdc() as the...
3
by: =?Utf-8?B?UnVieSBOYWRsZXI=?= | last post by:
Hi EveryOne, Does some one knows a way or tool that knows how to divide image into regions with c# so if the image is like a table style i will now the positions of the columns? i can read the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.