重新大小的图片不能正常工作C# [英] Re size image not working C#

查看:136
本文介绍了重新大小的图片不能正常工作C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上午重新上浆用下面的code图片

Am re sizing an image with the following code

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);
   }
}

但不给予任何质量的图像。像素化正在使图像有线。

but it is not giving an image of any quality. pixelation is making the image wired.

我也试过code

<一个href=\"http://stackoverflow.com/questions/6170912/how-can-i-get-better-results-when-shrinking-an-image/6171145?noredirect=1#comment24752853_6171145\">image重新大小的堆栈

但我得到一个黑色的屏幕作为结果,而不是JPG现在用PNG是唯一的区别。

but am getting a black screen as the result instead of jpg am using png is the only difference.

有改善图像质量的任何建议。我要重新大小透明图像的大小100by50。

any suggestion for improving the image quality. i have to re size the transparent image to a size 100by50.

先谢谢了。

推荐答案

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

Try this, Assuming you can use it

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;
}

这篇关于重新大小的图片不能正常工作C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆