加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – .net Drawing.Graphics.FromImage()返回空白的黑色图

发布时间:2020-12-16 06:57:28 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试在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
我正在尝试在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我给它的任何文件.
虽然如果我将输入图像流立即带入完成的位图,它会重新压缩并保存它,如:

Image original = Image.FromStream(myPostedFile.InputStream);
using (Bitmap done = new Bitmap(original))
{
 done.Save( Server.MapPath(saveas),ImageFormat.Jpeg );
}

我必须用g制作一些魔法吗?

UPD:
我试过了:

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,w,h); //also tried g.DrawImage(b,h)
  using (Bitmap done = new Bitmap(w,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,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);

希望这可以帮助迈拉

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读