如何调整不同分辨率的图像 [英] How to resize image with different resolution

查看:204
本文介绍了如何调整不同分辨率的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示照片库@宽度= 200 HEIGHT = 180的图像,上传图片我不得不调整它,但问题是每一个形象有不同的分辨率。我如何可以调整不同分辨率的图像,使图像保持不变。
这里是我的代码:

I have to display image in photo gallery @ width=200 height=180, but while uploading images I have to resize it , but the problem is every image have different resolution. How can I resize the images with different resolution so that images remain intact. Here is my code :

private void ResizeImage()
{
    System.Drawing.Image ImageToUpload = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    byte[] image = null;
    int h = ImageToUpload.Height;
    int w = ImageToUpload.Width;
    int r = int.Parse(ImageToUpload.VerticalResolution.ToString());
    int NewWidth = 200;//constant
    int NewHeight = 180;//constant
    byte[] imagesize = FileUpload1.FileBytes;
    System.Drawing.Bitmap BitMapImage = new System.Drawing.Bitmap(ImageToUpload, NewWidth, NewHeight);//this line gives horrible output
    MemoryStream Memory = new MemoryStream();
    BitMapImage.Save(Memory, System.Drawing.Imaging.ImageFormat.Jpeg);
    Memory.Position = 0;
    image = new byte[Memory.Length + 1];
    Memory.Read(image, 0, image.Length);
}



如果分辨率为96,如果我设置了maxWidth = 200那么它的高度将是150则只有图像看起来小,准确。我们不能调整图像中期望的方式,使它看起来准确?

if resolution is 96 and if I set maxwidth=200 then its height would be 150 then only the image looks small and accurate. Can't we resize image in desired way so that it looks exact?

推荐答案

试试这个,该功能将调整图像纵横维修器材比。

Try this, the function will resize image maintaing aspect ratio.

使用它像

Image BitMapImage = Resize(ImageToUpload, NewWidth, NewHeight);



函数来rezise图片

Function to rezise image

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

这篇关于如何调整不同分辨率的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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