C# 里怎么改变图片的分辨率
我想把要处理的图片大小统一成宽高为(22.40),该用什么方法来实现?1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。...
我想把要处理的图片大小统一成宽高为(22.40),该用什么方法来实现?
1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。 展开
1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。 展开
2015-04-01 · 知道合伙人互联网行家
关注
展开全部
代码如下:
bool ChangeImageSize(string imgPath, int width, long maxFileSize)
{
var bmp = (Bitmap)Image.FromFile(imgPath);
Size s = new Size(width, width * bmp.Height / bmp.Width);
var newBmp = new Bitmap(bmp, s);
var ms = TrySaveJpeg(newBmp, maxFileSize);
newBmp.Dispose();
bmp.Dispose();
if (ms==null) return false;
FileStream fs = new FileStream(imgPath, FileMode.Create);
ms.CopyTo(fs);
fs.Close();
return true;
}
Stream TrySaveJpeg(Bitmap bitmap, long maxByteSize)
{
MemoryStream msOld = new MemoryStream(),
msNew = new MemoryStream();
var codec = ImageCodecInfo.GetImageEncoders()
.FirstOrDefault(c=>c.MimeType=="image/jpeg");
var encParam = new EncoderParameters(1);
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
bitmap.Save(msOld, codec, encParam);
if (msOld.Length > maxByteSize){
return null; //最差画质
}
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bitmap.Save(msNew, codec, encParam);
if (msNew.Length < maxByteSize){
msNew.Seek(0, SeekOrigin.Begin);
return msNew; //最佳画质
}
//假设文件大小和图像质量成正相关 这里没有具体测试 姑且认为成立
//用二分法找到最合适的图像质量
long start=1, end=99;
while (start<end){
long qua=(start+end)/2;
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qua);
msNew.SetLength(0);
bitmap.Save(msNew, codec, encParam);
if (msNew.Length==maxByteSize){
msNew.Seek(0, SeekOrigin.Begin);
return msNew;
} else if (msNew.Length>maxByteSize){
end = qua-1;
} else if (msNew.Length<maxByteSize){
start = qua+1;
MemoryStream temp = msNew;
msNew = msOld;
msOld = temp; //保存一个小于指定大小 并且最接近的
}
}
msOld.Seek(0, SeekOrigin.Begin);
return msOld;
}
bool ChangeImageSize(string imgPath, int width, long maxFileSize)
{
var bmp = (Bitmap)Image.FromFile(imgPath);
Size s = new Size(width, width * bmp.Height / bmp.Width);
var newBmp = new Bitmap(bmp, s);
var ms = TrySaveJpeg(newBmp, maxFileSize);
newBmp.Dispose();
bmp.Dispose();
if (ms==null) return false;
FileStream fs = new FileStream(imgPath, FileMode.Create);
ms.CopyTo(fs);
fs.Close();
return true;
}
Stream TrySaveJpeg(Bitmap bitmap, long maxByteSize)
{
MemoryStream msOld = new MemoryStream(),
msNew = new MemoryStream();
var codec = ImageCodecInfo.GetImageEncoders()
.FirstOrDefault(c=>c.MimeType=="image/jpeg");
var encParam = new EncoderParameters(1);
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
bitmap.Save(msOld, codec, encParam);
if (msOld.Length > maxByteSize){
return null; //最差画质
}
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bitmap.Save(msNew, codec, encParam);
if (msNew.Length < maxByteSize){
msNew.Seek(0, SeekOrigin.Begin);
return msNew; //最佳画质
}
//假设文件大小和图像质量成正相关 这里没有具体测试 姑且认为成立
//用二分法找到最合适的图像质量
long start=1, end=99;
while (start<end){
long qua=(start+end)/2;
encParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qua);
msNew.SetLength(0);
bitmap.Save(msNew, codec, encParam);
if (msNew.Length==maxByteSize){
msNew.Seek(0, SeekOrigin.Begin);
return msNew;
} else if (msNew.Length>maxByteSize){
end = qua-1;
} else if (msNew.Length<maxByteSize){
start = qua+1;
MemoryStream temp = msNew;
msNew = msOld;
msOld = temp; //保存一个小于指定大小 并且最接近的
}
}
msOld.Seek(0, SeekOrigin.Begin);
return msOld;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询