C# 里怎么改变图片的分辨率
发布网友
发布时间:2023-09-01 20:07
我来回答
共5个回答
热心网友
时间:2024-01-07 22:51
把图片框PictureBox调成22.4*22.4固定大小的,
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
这样就OK了,NBX
热心网友
时间:2024-01-07 22:51
把图片框PictureBox调成22.4*22.4固定大小的,
PictureBox.Size 设置成 22.4*22.4
PictureBox.SizeMode 设置成 StretchImage
热心网友
时间:2024-01-07 22:52
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);
}
热心网友
时间:2024-01-07 22:52
太简单了,
g.drawImage(img,0f,0f,22.40,22.40)
热心网友
时间:2024-01-07 22:53
代码如下:
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;
}