【问题标题】:How do I resize an image without stretching it?如何在不拉伸图像的情况下调整图像大小?
【发布时间】:2013-06-18 18:51:00
【问题描述】:

我正在尝试在 C# 中调整图像(位图)的大小而不拉伸图像。

假设图像是100x100 像素。

我希望将其设为100x110 像素,并在图像底部留下一个白色间隙,它添加了额外的像素。

我已经这样做了,但找不到指定像素格式的方法。我需要它是8bppindexed。我附上了一个例子来显示之前和之后的图像。

这是我目前的代码。

string visit2 = "C:\\users\\moorez\\desktop\\visit2.bmp";

Bitmap orig = new Bitmap(visit2);
int width = orig.Width;
int height = orig.Height;
int newHeight = height + 2;
Bitmap newImage = orig.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
newImage.Save("C:\\users\\moorez\\desktop\\visit3.bmp");

Bitmap test = new Bitmap(width, newHeight);
Graphics g = Graphics.FromImage(test);
g.DrawImage(newImage, new Point(0, 0));
test.Save("C:\\users\\moorez\\desktop\\visit4.bmp");

【问题讨论】:

  • 请出示您的代码。目前尚不清楚您使用什么方法来创建位图以及如何在其上绘制另一个位图来帮助您。
  • GDI+ 不支持绘制成 8bpp 图像。这是非常重要的,将色深从 1600 万色降低到 256 色会留下很多令人不快的伪影。你需要另一个图形库,我认为 AForge 可以做到。最好的事情就是不要打扰,20 年前 8bpp 很重要,当时 640 KB 的内存就足够了。
  • @HansPassant 我不想使用这种格式,但是还有另一个程序将在需要它的图像上运行。不过谢谢,我会研究 AForge。
  • 我希望 SO 用户能够记录这些“其他程序”。所以我们知道要远离他们。
  • @HansPassant 如果这是一个副项目,我会的,但这是为了工作,该程序不向公众开放。对不起。

标签: c# bitmap system.drawing


【解决方案1】:

你可以试试这个

Bitmap bmp = new Bitmap(newImage.Width, newHeight);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawImageUnscaled(newImage, 0, 0, newImage.Width, newHeight);
bmp.Save(@"C:\\users\\moorez\\desktop\\visit3.bmp", ImageFormat.Jpeg);

【讨论】:

  • "我需要它是 8bppindexed"
猜你喜欢
  • 1970-01-01
  • 2018-09-25
  • 2014-04-07
  • 2012-07-01
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 2015-05-04
相关资源
最近更新 更多