[英]The output image after cropping is larger than the original size
When I crop an Image using Bitmap.Clone() it creates output larger than the original image Size 当我使用Bitmap.Clone()裁剪图像时,其创建的输出大于原始图像的大小
The Original size is : 5 M 原始大小为:5 M
and the Output Cropped image is : 28 M 并且输出的裁剪图像为:28 M
How can I make cropping without losing quality and with no large size? 如何在不损失质量且没有大尺寸的情况下进行裁剪? My code is : 我的代码是:
private static Image cropImage(Image img, Rectangle cropArea)
{
var bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
img.Dispose();
bmpCrop.Save(@"D:\Work\CropImage\CropImage\crop.jpg",bmpImage.RawFormat );
return (Image)(bmpCrop);
}
static void Main(string[] args)
{
string sourceimg = @"D:\Work\Crop Image\CropImage\4032x5808.jpg";
Image imageoriginal = Image.FromFile(sourceimg);
int HorX,HorY,VerX,VerY;
Console.WriteLine("Enter X 1 Cor. ");
HorX=int.Parse(Console.ReadLine());
Console.WriteLine("Enter Y 1 Cor. ");
HorY=int.Parse(Console.ReadLine());
Console.WriteLine("Enter X 2 Cor. ");
VerX=int.Parse(Console.ReadLine());
Console.WriteLine("Enter Y 1 Cor. ");
VerY= int.Parse(Console.ReadLine());
Rectangle rect = new Rectangle(HorX,HorY,VerX,VerY);
cropImage(imageoriginal, rect);
}
You are saving the image as a bitmap, not a jpg, or png which is probably your input format. 您正在将图像另存为位图,而不是jpg或png(可能是您的输入格式)。 Change the format type and you will see the file size plummet! 更改格式类型,您将看到文件大小直线下降!
This code sample should help you set the quality:- 此代码示例应帮助您设置质量:-
var qualityEncoder = Encoder.Quality;
var quality = (long)<desired quality>;
var ratio = new EncoderParameter(qualityEncoder, quality );
var codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;
var jpegCodecInfo = <one of the codec infos from ImageCodecInfo.GetImageEncoders() with mime type = "image/jpeg">;
bmp.Save(fileName, jpegCodecInfo, codecParams); // Save to JPG
Usually you don't need to worry about reducing quality when cropping as you simply save part of the original image with 1:1 ratio. 通常,您不必担心裁剪时降低质量,因为您只需以1:1的比例保存原始图像的一部分即可。
Your sample works fine - problem must be in Rectangle
input part. 您的示例工作正常-问题必须在“ Rectangle
输入部分中。 As your variable names are confusing I would like to recommend reading about it at MSDN . 由于您的变量名令人困惑,因此我建议您在MSDN上阅读有关它的信息。
To be sure, just initialize your rectangle (assume that your image is greater than 1000x900 ) with new Rectangle(200,100,800,800);
可以肯定的是,只需使用new Rectangle(200,100,800,800);
200,100,800,800)初始化矩形(假设您的图像大于1000x900 new Rectangle(200,100,800,800);
This will crop region starting from 200
th pixel from the left and 100
th pixel from the top with width and height equal to 800
pixels. 这将裁剪区域,该区域从左侧的第200
个像素开始,从顶部的第100
个像素开始,宽度和高度等于800
像素。
Final quality will remain the same and image size will be smaller. 最终质量将保持不变,并且图像尺寸将变小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.