C# 里怎么改变图片的分辨率
我想把要处理的图片大小统一成宽高为(22.40),该用什么方法来实现?1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。...
我想把要处理的图片大小统一成宽高为(22.40),该用什么方法来实现?
1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。 展开
1,3方案都不行,是宽为22,高为40,第二的能不能清楚点啊,不要直接复制MSDN。。。。 展开
展开全部
把图片框PictureBox调成22.4*22.4固定大小的,
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
这样就OK了,NBX
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
这样就OK了,NBX
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把图片框PictureBox调成22.4*22.4固定大小的,
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Image.GetThumbnailImage()
public bool ThumbnailCallback()
{
return false;
}
public void Example_GetThumb(PaintEventArgs e)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap("Climber.jpg");
Image myThumbnail = myBitmap.GetThumbnailImage(
40, 40, myCallback, IntPtr.Zero);
e.Graphics.DrawImage(myThumbnail, 150, 75);
}
public bool ThumbnailCallback()
{
return false;
}
public void Example_GetThumb(PaintEventArgs e)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap("Climber.jpg");
Image myThumbnail = myBitmap.GetThumbnailImage(
40, 40, myCallback, IntPtr.Zero);
e.Graphics.DrawImage(myThumbnail, 150, 75);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
太简单了,
g.drawImage(img,0f,0f,22.40,22.40)
g.drawImage(img,0f,0f,22.40,22.40)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码如下:
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |