Clip a Graphics region

  • Thread starter Thread starter Manuel
  • Start date Start date
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
 
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();
 
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();
 
Back
Top