Clip a Graphics region

M

Manuel

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
 
G

Guest

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();
 
M

Manuel

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();
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top