gpt4 book ai didi

c# - 调整图像大小 gdi+ graphics .net

转载 作者:行者123 更新时间:2023-11-30 14:04:35 26 4
gpt4 key购买 nike

我有这种方法可以缩小我正在处理的网站的图像:

static byte[] createSmallerImage(
BlogPhoto blogPhoto,
int newMaxWidth,
int newMaxHeight)
{
Image img;
using (MemoryStream originalImage =
new MemoryStream(blogPhoto.BlogPhotoImage))
{
img = Image.FromStream(originalImage);
}

int newWidth;
int newHeight;
byte[] arr;

if (img.Width > img.Height)
{
if (img.Width <= newMaxWidth)
{

using (MemoryStream thumbStr = new MemoryStream())
{
img.Save(thumbStr, ImageFormat.Jpeg);
img.Dispose();
arr = thumbStr.ToArray();
}
return arr;
}

newWidth = newMaxWidth;
newHeight =
(int)(((float)newWidth / (float)img.Width) * (float)img.Height);
}
else
{
if (img.Height <= newMaxHeight)
{

using (MemoryStream thumbStr = new MemoryStream())
{
img.Save(thumbStr, ImageFormat.Jpeg);
img.Dispose();
arr = thumbStr.ToArray();
}
return arr;
}

newHeight = newMaxHeight;
newWidth =
(int)(((float)newHeight / (float)img.Height) * (float)img.Width);
}

Image thumb = new Bitmap(newWidth, newHeight);

Graphics g = Graphics.FromImage(thumb);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;

g.DrawImage(img, 0f, 0f, (float)newWidth, (float)newHeight);


using (MemoryStream thumbStr = new MemoryStream())
{
thumb.Save(thumbStr, ImageFormat.Jpeg);
arr = thumbStr.ToArray();
}

g.Dispose();
img.Dispose();

return arr;
}

大多数时候它工作得很好,但有时它会给我这个异常:GDI+ 中发生一般性错误。错误代码 -2147467259。资料来源:“System.Drawing”。这发生在 Image.Save(... 我试图使这段代码尽可能具有防御性,但仍然没有得到导致这种情况的原因。如果有人知道答案会很好,也欢迎批评。

最佳答案

我个人使用这段代码,没有流(不过我不关心性能)来调整图片大小:

public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;

//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;

newWidth = newHeight;
newHeight = buff;
}

int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;

nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);


Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);

bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}

希望这对您有所帮助。

关于c# - 调整图像大小 gdi+ graphics .net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1582499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com