How to Crop an Image Using C#

How do I crop an image using C#?

You can use Graphics.DrawImage to draw a cropped image onto the graphics object from a bitmap.

Rectangle cropRect = new Rectangle(...);
Bitmap src = Image.FromFile(fileName) as Bitmap;
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}

How to cut a part of image in C#

Check out the Graphics Class on MSDN.

Here's an example that will point you in the right direction (notice the Rectangle object):

public Bitmap CropImage(Bitmap source, Rectangle section)
{
var bitmap = new Bitmap(section.Width, section.Height);
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bitmap;
}
}

// Example use:
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));

Bitmap CroppedImage = CropImage(source, section);

Cropping an image from file and resaving in C#

private void croptoSquare(string date)
{
//Location of 320x240 image
string fileName = Server.MapPath("~/Content/images/" + date + "contactimage.jpg");

// Create a new image at the cropped size
Bitmap cropped = new Bitmap(240,240);

//Load image from file
using (Image image = Image.FromFile(fileName))
{
// Create a Graphics object to do the drawing, *with the new bitmap as the target*
using (Graphics g = Graphics.FromImage(cropped) )
{
// Draw the desired area of the original into the graphics object
g.DrawImage(image, new Rectangle(0, 0, 240, 240), new Rectangle(40, 0, 240, 240), GraphicsUnit.Pixel);
fileName = Server.MapPath("~/Content/images/" + date + "contactimagecropped.jpg");
// Save the result
cropped.Save(fileName);
}
}

}

C# Crop Image Using Co-ordinates

After getting a full understanding of what I was doing incorrectly. I then modified the code. I was writing to the same File Stream, so it was not actually saving the cropped image. Changed the code to write to a new File Stream and the cropped image is now being saved.

Byte[] data;

using (FileStream fs = new FileStream(fileNameWitPath, fileMode))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
//get co-ords
int x1 = Convert.ToInt32(context.Request.QueryString["x1"].Trim());
int y1 = Convert.ToInt32(context.Request.QueryString["y1"].Trim());
int x2 = Convert.ToInt32(context.Request.QueryString["x2"].Trim());
int y2 = Convert.ToInt32(context.Request.QueryString["y2"].Trim());

Bitmap b = new Bitmap(fs);
Bitmap nb = new Bitmap((x2 - x1), (y2 - y1));
Graphics g = Graphics.FromImage(nb);
//g.DrawImage(b, x2, y2);
Rectangle cropRect = new Rectangle(x1, y1, nb.Width, nb.Height);

g.DrawImage(b, new Rectangle(0, 0, nb.Width, nb.Height), cropRect, GraphicsUnit.Pixel);

using (var memoryStream = new MemoryStream())
{
nb.Save(memoryStream, ImageFormat.Png);
data = memoryStream.ToArray();
}

bw.Close();
}
}

using (FileStream fs = new FileStream(fileNameWitPath, fileMode))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(data);
bw.Close();
}
}

how to crop image with polygon in c#

You would create a new bitmap, at least as large as the bounding box of your polygon. Create a graphics object from this new bitmap. You can then draw the polygon to this bitmap, using the original image as a texture brush. Note that you might need to apply transform matrix to translate from the full image coordinates to the cropped image coordinates.

Note that it looks like you have radiological images. These are typically 16 bit images, so they will need to be converted to 8bit mono, or 24bit RGB before they can be used. This should already be done in the drawing code if you have access to the source. Or you can do it yourself.

How to crop image into a square

This is the code I have always used in my websites:

 public Bitmap MakeSquarePhoto(Bitmap bmp, int size)
{
try
{
Bitmap res = new Bitmap(size, size);
Graphics g = Graphics.FromImage(res);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, size, size);
int t = 0, l = 0;
if (bmp.Height > bmp.Width)
t = (bmp.Height - bmp.Width) / 2;
else
l = (bmp.Width - bmp.Height) / 2;
g.DrawImage(bmp, new Rectangle(0, 0, size, size), new Rectangle(l, t, bmp.Width - l * 2, bmp.Height - t * 2), GraphicsUnit.Pixel);
return res;
}
catch { }
}


Related Topics



Leave a reply



Submit