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;
}
Related
I seem to be having trouble up-scaling an image that is 8x8 pixels. When testing, I wanted to scale the image up to 64x64 pixels. However, when doing so, this was the result:
Rescaling it the way I did below, removes 4 pixels height on the top, and 4 pixels width on the left, and adds 4 black pixels height on the bottom, and 4 pixels width on the right.
Here is the code I am using to rescale the image:
private static Image ScaleImage(string username, int size)
{
Image avatar = MergeImage(username);
int originalWidth = avatar.Width;
int originalHeight = avatar.Height;
float ratioX = (float)size / (float)originalWidth;
float ratioY = (float)size / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format48bppRgb);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(avatar, 0, 0, newWidth, newHeight);
}
return newImage;
}
I am unsure of what is going wrong here. Any help would be greatly appreciated.
Add this before g.DrawImage:
g.PixelOffsetMode = PixelOffsetMode.Half;
i generate picture according to the size of picture box and set the picture to picture box whose size mode is normal but full image is not showing rather few area of picture is cutting off. i want to generate picture in such a way as a result when i will set the picture on picture box then full image should be display. here is my code by which i generate picture
new Bitmap _lastSnapshot = new Bitmap(261, 204);
this.DrawToBitmap((Bitmap)_lastSnapshot, new Rectangle(Point.Empty, ((Bitmap)_lastSnapshot).Size));
261, 204 is size of picture box and picture size mode is normal.i assign the lastSnapshot to picture box after generation but full picture is not displaying.
i got a routine to resize image according to picture box size. it works well but image looks become obscure or unclear.i have to set the picturebox size mode stretch to fill up the image into pic box.
here is the routine i use to resize picture according to picture box size.
public static Image ResizeImage(Image image, Size size,
bool preserveAspectRatio = true)
{
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)size.Width / (float)originalWidth;
float percentHeight = (float)size.Height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
call the routine
ResizeImage(value,pictureBox1.Size,true);
can anyone give some advise to generate and resize the picture for fit into picture box with good crystal clear image. thanks
If you want to preview of image in picturebox according to picturebox size then:
Change PictureBox property SizeMode to Zoom.
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
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);
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
I have this code to resize an image but the image doesn't look so good:
public Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int maxHeight)
{
// original dimensions
int w = src.Width;
int h = src.Height;
// Longest and shortest dimension
int longestDimension = (w > h) ? w : h;
int shortestDimension = (w < h) ? w : h;
// propotionality
float factor = ((float)longestDimension) / shortestDimension;
// default width is greater than height
double newWidth = maxWidth;
double newHeight = maxWidth / factor;
// if height greater than width recalculate
if (w < h)
{
newWidth = maxHeight / factor;
newHeight = maxHeight;
}
// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
return result;
}
Try setting the InterpolationMode of the graphics object to some value such as HighQualityBicubic. This should ensure that resize/scaled image looks much better than the "default".
So, in the code you have posted, instead of doing:
// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
return result;
Try doing this instead:
// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
}
return result;
(Note the line setting the InterpolationMode property to one of the InterpolationMode enumeration's values).
Please see this link:
How to: Use Interpolation Mode to Control Image Quality During Scaling
For further information on controlling the quality of an image when resizing/scaling.
Also see this CodeProject article:
Resizing a Photographic image with GDI+ for .NET
For information of the different visual effects the various InterpolationMode enumeration settings will have on an image. (About two-thirds of the way down the article, under the section entitled, "One last thing...").
If you need an on-the-fly resize I suggest you to try an HttpHandler I write recently, I've posted the code on my blog (sorry, but is in italian).
With some modifies you can use the code also for save the transformed image on the disk.