Developers Forum :: Graphics
Welcome Guest   [Login]
 Subject :Copy a part of image to new bitmap.. 01-02-2010 11:00:14 
CBud
Joined: 01-02-2010 09:30:21
Posts: 1
Location

Hello, I've got a problem, hope you can help me.

 

For an application i'm building, I have to copy a part of an bitmap (1) to another bitmap (2). Bitmap 1 is a large image i can read from file with:

dim imgLarge as image

imgLarge = System.Drawing.Image.FromFile("someimage.jpg)

 

Bitmap 2 is the bitmap where I want to copy a part of Bitmap 1 to. Bitmap 2 I have to create from scratch, and is always smalller than Bitmap 1. For Bitmap 2 I have the size it has to be: myWidth and myHeight. I also know which section I have to copy from Bitmap 1: the section has dimensions (myWidth, myHeight) and starts at (myLeft, myTop).

 

IP Logged
 Subject :Re:Copy a part of image to new bitmap.. 03-02-2010 13:14:30 
admin
Joined: 16-11-2009 16:26:59
Posts: 1
Location: The Netherlands
 

You can use the DrawImage method: suppose you have read your image into the bitmap MyBitmap. Copy the rectangle (x,y,width,height) to a new image with:

Dim BitmapToUse As New Bitmap(Width, Height )

Dim img As Image = Image.FromHbitmap(BitmapToUse.GetHbitmap)


Dim g As Graphics = Graphics.FromImage(img)

Dim SrcRectangle As Rectangle = New Rectangle(x, y, Width, Height)
Dim TargetRectangle As Rectangle = New Rectangle(0, 0, Width, Height)


g.DrawImage(MyBitmap, TargetRectangle, SrcRectangle, GraphicsUnit.Pixel)

Now img contains only the desired rectangle

You can even use this technique to enlarge of shrink the original image: just adjust the size of the Rectangle.

 

IP Logged
Page # 


Powered by ccBoard