C#. Crop image on N rectangles - c#

I need to crop image on N rectangles.
Condition for all rectangles:
Rectangle's width = X
Rectangle's height = X/2
Any algorithm ?

This can help you:
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
I found a more complex but complete example here:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e0c30a26-60da-457d-a217-ba95650deec8

Related

How to crop bitmap and get the lower 50% of the Image

I have an Image which I want to crop. I already know that my required content is in the lower part of the image so how can I automatically crop it using Rectangle ?
I tried to use this Code (Below) to crop but it didn't help. I haven't used Rectangle much so please guide me.
private Bitmap cropImage(Bitmap img)
{
int height= img.Height / 2;
Rectangle CropArea = new Rectangle(100,height, 1400, 900);
Bitmap bmpCrop = img.Clone(CropArea, img.PixelFormat);
return bmpCrop;
}
the cropped image turned out fine but it's hardcoded. I need to make it dynamic so it gives same result for different images
Thanks #John for your answer. this code will cut 50% of the image and give your it's lower half:
private Bitmap cropImage(Bitmap img)
{
int height= img.Height / 2;
int newWidth = img.Width -100;
int newHeight = img.Height - height;
Rectangle CropArea = new Rectangle(100,height,newWidth,newHeight);
Bitmap bmpCrop = img.Clone(CropArea, img.PixelFormat);
return bmpCrop;
}

Cropping Image(From Screen)

I have read many question about this. But my question is little bit different. What i need to do crop image from screen.
There is my codes
Bitmap photo = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.primaryScreen.Bounds.Height)
Graphics gr = Graphics.FromImage(photo);
gr.CopyFromScreen(0,0,0,0 new size(foto.Width,foto.Height));
picturebox1.Image = photo;
And there my crop codes
Rectangle cropRec = new Rectangle(1,1,1,1);
Bitmap target = new Bitmap(cropRec.Width,cropRec.Height);
using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(photo,new Rentangle(0,0,target.Width,target.Height),cropRec,GraphicsUnit.Pixel);
}
I want to crop middle part of this photo and compare with itself.
Thanks n advance
To symmetrically crop your image as shown in the following sketch, try creating a Bitmap with margins in the X and Y axis and then cropping the screenshot image by using those margins in CopyFromScreen():
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width - 2 * xmargin, Screen.PrimaryScreen.Bounds.Height - 2 * ymargin);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(xmargin, ymargin, 0, 0, printscreen.Size);

Image cropping issue in C#

In my asp.net C# application, I am trying to crop the below image:
I have selected only the face part, but the cropped image always selecting from top left corner like below:
I am using the below code to crop image:
Rectangle sourceRect = new Rectangle(iX1, iY1, w, h);
System.Drawing.Image imgNew = CropImage(imgOrig, sourceRect);
private static System.Drawing.Image CropImage(System.Drawing.Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
img.Dispose();
return (System.Drawing.Image)bmpCrop;
}
Use this code to crop your image.
static Bitmap CropImage(Image originalImage, Rectangle sourceRectangle, Rectangle destinationRectangle)
{
var croppedImage = new Bitmap(destinationRectangle.Width, destinationRectangle.Height);
using (var graphics = Graphics.FromImage(croppedImage))
{
graphics.DrawImage(originalImage, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return croppedImage;
}
The problem is that your original algorithm doesn't specify where to start cropping from. It therefore always starts from the origin, which is not what you want.

Cropping Images .Net

I am trying to do a simple crop of an image, but for some reason it is not respecting my starting x,y location. It is always starting the crop at 0,0. The following is what I am doing:
Bitmap original = new Bitmap(pictureBox1.Image);
int x = Convert.ToInt32(txtX.Text);
int y = Convert.ToInt32(txtY.Text);
int dX = Convert.ToInt32(txtDeltaX.Text);
int dY = Convert.ToInt32(txtDeltaY.Text);
Point loc = new Point(x, y);
Size cropSize = new Size(dX, dY);
Rectangle cropArea = new Rectangle(loc, cropSize);
Bitmap bmpCrop = CropImage(original, cropArea);
pictureBox1.Image = bmpCrop;
The cropping method:
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
This should be very simple, but for some reason its not working. Its cropping it, just at 0,0.
Thanks!
You should try to use
g.DrawImage(source, section);
Anyway this function works:
public Bitmap CropBitmap(Bitmap bitmap,
int cropX, int cropY,
int cropWidth, int cropHeight)
{
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
return cropped;
}
You might want to use Graphics.DrawImageUnscaledAndClipped
Try to use, something like this :
g.DrawImage(source, x, y, section, GraphicsUnit.Pixel);

Crop an image in C#

I am doing sort of a limited graphics editor in a desktop application in c# 3.5 GDI. the user first selects an image which is shown in a picturebox control which is smaller in size so image resizing is done to fit the picture.
For cropping, the user selects the area to crop. there are a number of example on the net that explains how to crop the image but none of them explains the case when the area is selected on a thumbnail but the cropping is done on the original image i.e. some kind of mapping is done between the two images.
all the graphic editor provide similar functionality. can you direct me to a link which explains how to do this?
Sounds to me like you need to calculate the crop rectangle on the original image yourself based on the relative sizes of the picture and the thumbnail.
public static class CoordinateTransformationHelper
{
public static Point ThumbToOriginal(this Point point, Size thumb, Size source)
{
Point rc = new Point();
rc.X = (int)((double)point.X / thumb.Width * source.Width);
rc.Y = (int)((double)point.Y / thumb.Height * source.Height);
return rc;
}
public static Size ThumbToOriginal(this Size size, Size thumb, Size source)
{
Point pt = new Point(size);
Size rc = new Size(pt.ThumbToOriginal(thumb, source));
return rc;
}
public static Rectangle ThumbToOriginal(this Rectangle rect, Size thumb, Size source)
{
Rectangle rc = new Rectangle();
rc.Location = rect.Location.ThumbToOriginal(thumb, source);
rc.Size = rect.Size.ThumbToOriginal(thumb, source);
return rc;
}
}
Usage example:
Size thumb = new Size(10, 10);
Size source = new Size(100, 100);
Console.WriteLine(new Point(4, 4).ThumbToOriginal(thumb, source));
Console.WriteLine(new Rectangle(4, 4, 5, 5).ThumbToOriginal(thumb, source));
here's a really easy method to crop a System.Drawing.Image
public static Image CropImage(Image image, Rectangle area)
{
Image cropped = null;
using (Bitmap i = new Bitmap(image))
using (Bitmap c = i.Clone(area, i.PixelFormat))
cropped = (Image)c;
return cropped;
}
pass in an Image and the area that you want to crop and that should do it

Categories