gpt4 book ai didi

asp.net - .net Drawing.Graphics.FromImage() 返回空白黑色图像

转载 作者:行者123 更新时间:2023-12-04 19:21:35 24 4
gpt4 key购买 nike

我正在尝试在 asp.net 中重新调整上传的 jpeg

所以我去:

Image original = Image.FromStream(myPostedFile.InputStream);
int w=original.Width, h=original.Height;

using(Graphics g = Graphics.FromImage(original))
{
g.ScaleTransform(0.5f, 0.5f); ... // e.g.
using (Bitmap done = new Bitmap(w, h, g))
{
done.Save( Server.MapPath(saveas), ImageFormat.Jpeg );
//saves blank black, though with correct width and height
}
}

无论我给它什么文件,这都会保存一个原始的黑色 jpeg。
虽然如果我立即将输入图像流带入 done位图,它会重新压缩并保存它,例如:
Image original = Image.FromStream(myPostedFile.InputStream);
using (Bitmap done = new Bitmap(original))
{
done.Save( Server.MapPath(saveas), ImageFormat.Jpeg );
}

我必须用 g 做一些魔法吗?

更新:
我试过:
Image original = Image.FromStream(fstream);
int w=original.Width, h=original.Height;
using(Bitmap b = new Bitmap(original)) //also tried new Bitmap(w,h)
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage(original, 0, 0, w, h); //also tried g.DrawImage(b, 0, 0, w, h)
using (Bitmap done = new Bitmap(w, h, g))
{
done.Save( Server.MapPath(saveas), ImageFormat.Jpeg );
}
}

相同的故事 - 正确尺寸的纯黑色

最佳答案

既然你没有 用图像背景填充区域你正在从 inputStream 读取,你只能以这种方式获得一个空白图像。

您可以使用将背景填充到调整大小的区域中,而不是使用缩放图像。

看一下这个:

Image img = Image.FromFile(Server.MapPath("a.png"));
int w = img.Width;
int h = img.Height;

//Create an empty bitmap with scaled size,here half
Bitmap bmp = new Bitmap(w / 2, h / 2);
//Create graphics object to draw
Graphics g = Graphics.FromImage(bmp);
//You can also use SmoothingMode,CompositingMode and CompositingQuality
//of Graphics object to preserve saving options for new image.

//Create drawing area with a rectangle
Rectangle drect = new Rectangle(0, 0, bmp.Width, bmp.Height);
//Draw image into your rectangle area
g.DrawImage(img, drect);
//Save your new image
bmp.Save(Server.MapPath("a2.jpg"), ImageFormat.Jpeg);

希望这可以帮助
迈拉

关于asp.net - .net Drawing.Graphics.FromImage() 返回空白黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957913/

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