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

Clip a Graphics region

P: n/a
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 advice
Jun 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Nigel,

This article can be useful: http://www.dotnet247.com/247referenc...29/145049.aspx

Regards,
Alex
http://devkids.blogspot.com
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 advice

Jun 10 '07 #2

P: n/a
On Jun 10, 3:04 pm, Manuel <vanilla...@the.skywrote:
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 advice
x1 - starting X
y1 - starting Y
newHeight, newWidth - new dimensions

Bitmap sourceBitmap1 = new Bitmap(stream);
Color col;

Bitmap sourceBitmap2 = new Bitmap(newWidth, newHeight);
for (int i = x1; i <= newWidth - 1; i++)
{
for (int j = y1; j <= newHeight - 1; j++)
{
col = sourceBitmap1.GetPixel(i, j);
sourceBitmap2.SetPixel(i - x1, j - y1, col);
}
}

Bitmap targetBitmap = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(targetBitmap);

Rectangle rect = new Rectangle(0, 0, newWidth, newHeight);

g.DrawImage(sourceBitmap2, rect);

targetBitmap.Save(filename);
g.Dispose();
targetBitmap.Dispose();

Jun 10 '07 #3

P: n/a
Manuel ha scritto:
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 advice
Thanks to all,
I have solved in this way:

System.Drawing.Image img_clipped = new Bitmap(256*2, 256);
Rectangle rec = new Rectangle(10, 10, 100, 200);
Graphics g_clipped = Graphics.FromImage(img_clipped);
/* Using rectangles */
RectangleF destRect = new Rectangle(0, 0, 200, 200);
RectangleF srcRect2 = new Rectangle(0, 0, 200, 200);
g_clipped.DrawImage(img, destRect, srcRect2, GraphicsUnit.Pixel);
g_clipped.Save();
Jun 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.