0

我正在使用以下代码调整图像大小

using (Image thumbnail = new Bitmap(100, 50))
{
  using (Bitmap source = new Bitmap(imageFile))
  {
    using (Graphics g = Graphics.FromImage(thumbnail))
    {
      g.CompositingQuality = CompositingQuality.HighQuality;
      g.InterpolationMode = InterpolationMode.HighQualityBicubic;
      g.SmoothingMode = SmoothingMode.HighQuality;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.InterpolationMode = InterpolationMode.HighQualityBicubic;
      g.DrawImage(source, 0, 0, 100, 50);
    }
  }
  using (MemoryStream ms = new MemoryStream())
   {
     thumbnail.Save(ms, ImageFormat.Png);
     thumbnail.Save(dest, ImageFormat.Png);
   }
}

但它没有给出任何质量的图像。像素化使图像连线。

我也试过代码

堆栈中的图像重新大小

但是结果是黑屏而不是 jpg 我使用 png 是唯一的区别。

任何提高图像质量的建议。我必须将透明图像的大小重新调整为 100×50。

提前致谢。

4

1 回答 1

2

试试这个,假设你可以使用它

public static Image Resize(Image originalImage, int w, int h)
{
    //Original Image attributes
    int originalWidth = originalImage.Width;
    int originalHeight = originalImage.Height;

    // Figure out the ratio
    double ratioX = (double)w / (double)originalWidth;
    double ratioY = (double)h / (double)originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    Image thumbnail = new Bitmap(newWidth, newHeight);
    Graphics graphic = Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    graphic.Clear(Color.Transparent);
    graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);

    return thumbnail;
}
于 2013-06-17T05:28:15.240 回答