Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算图像大小_C#_Image - Fatal编程技术网

C# 计算图像大小

C# 计算图像大小,c#,image,C#,Image,基本上,我有一个大的图像(高度和宽度都有几千像素),宽度和高度都有很大的变化 我需要做的是以大约500像素的高度和宽度显示这些图像-但我想保持这些图像的纵横比不变,因此这些值将非常接近。但是我有点困惑,我可以用什么计算原始的宽度和高度来得到正确的数字 有什么想法吗?试试这个: Size original = new Size(5000,4000); double ratio = (double)original.Height/original.Width; int resizePercent =

基本上,我有一个大的图像(高度和宽度都有几千像素),宽度和高度都有很大的变化

我需要做的是以大约500像素的高度和宽度显示这些图像-但我想保持这些图像的纵横比不变,因此这些值将非常接近。但是我有点困惑,我可以用什么计算原始的宽度和高度来得到正确的数字

有什么想法吗?

试试这个:

Size original = new Size(5000,4000);
double ratio = (double)original.Height/original.Width;
int resizePercent = 50;
int newWidth = Convert.ToInt32(original.Width*resizePercent/100);
int newHeight = Convert.ToInt32(newWidth * ratio);
Size resized = new Size(newWidth,newHeight);
当然,只需将原始变量设置为图像大小,并确定调整大小的百分比。如果目标宽度如下所示,则可以计算调整大小百分比:

int targetWidth = 500;
int resizePercent = Convert.ToInt32((double)original.Width/targetWidth);

几周前我遇到了这个问题。看看这篇文章

创建一个通用处理程序(.ashx),用于查找图像、调整图像大小并返回图像

以下是我最终使用的(您可以动态调整大小):


你看过这里吗?在哪个站台上展示?银灯?wpf?winforms?asp.net?(等)您的答案取决于此信息。您如何知道他希望显示在网页上?:)
public void ProcessRequest (HttpContext context) {
    TimeSpan refresh = new TimeSpan(168, 0, 0);
    HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
    HttpContext.Current.Response.Cache.SetMaxAge(refresh);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
    HttpContext.Current.Response.Cache.SetValidUntilExpires(true);

    string dir = HttpContext.Current.Request.QueryString["dir"];
    string img = HttpContext.Current.Request.QueryString["img"];
    bool force = HttpContext.Current.Request.QueryString["force"] == "yes";
    double w = 0;
    double h = 0;

    string path = null;
    Image orig = null;
    Image thumb = null;

    //make sure we have a directory and image filename
    if (string.IsNullOrEmpty(dir))
        return;

    //make sure that we allow access to that directory, could do some extra sneaky security stuff here if we had to... (only accept a DirectoryID so the user doesn't know the actual path)
    switch (dir)
    {
        case "user":
            dir = "assets/images/users";
            if (string.IsNullOrEmpty(img) || img == "undefined") img = "0.jpg";
            break;
            break;
        case "icon":
            dir = "assets/images/icons";
            break;
        default:
            dir = "assets/images";
            break;
    }

    //make sure that the image filename is just a filename
    if (img.IndexOf("/") > -1 | img.IndexOf("\\") > -1)
        return;

    //make sure the image exists
    path = HttpContext.Current.Server.MapPath("~/" + dir + "/" + img);
    if (System.IO.File.Exists(path))
    {
        orig = Image.FromFile(path);
    }
    else
    {
        return;
    }


    //if there is a max width or height
    if (double.TryParse(HttpContext.Current.Request.QueryString["w"], out w) | double.TryParse(HttpContext.Current.Request.QueryString["h"], out h))
    {
        //thumb is the resized image
        double s = 1;
        if (w > 0 & h > 0)
        {
            double ratio = h / w;
            if (orig.Height / (double)orig.Width > ratio)
            {
                if (orig.Height > h)
                {
                    if (force)
                        s = w / (double)orig.Width;
                    else
                        s = h / (double)orig.Height;
                }
            }
            else
            {
                if (orig.Width > w)
                {
                    if (force)
                        s = h / (double)orig.Height;
                    else
                        s = w / (double)orig.Width;
                }
            }
        }
        else if (w > 0)
        {
            if (orig.Width > w)
            {
                s = w / (double)orig.Width;
            }
        }
        else if (h > 0)
        {
            if (orig.Height > h)
            {
                s = h / (double)orig.Height;
            }
        }

        //double flip gets it to lose the embedded thumbnail
        //http://www.4guysfromrolla.com/articles/012203-1.2.aspx
        orig.RotateFlip(RotateFlipType.Rotate180FlipNone);
        orig.RotateFlip(RotateFlipType.Rotate180FlipNone);

        thumb = orig.GetThumbnailImage(Convert.ToInt32(orig.Width * s), Convert.ToInt32(orig.Height * s), null, IntPtr.Zero);

        if (force && w > 0 & h > 0)
            thumb = cropImage(thumb, (int)w, (int)h);
    }
    else
    {
        //thumb is the image at it's original size
        thumb = orig;
    }

    HttpContext.Current.Response.ContentType = "image/jpeg";
    thumb.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}


private Image cropImage(Image img, int w, int h)
{
    if (w > img.Width && h > img.Height)
        return img;
    Rectangle cropArea = new Rectangle((img.Width - w) / 2, (img.Height - h) / 2, w, h);
    Bitmap bmpImage = new Bitmap(img);
    Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
    return (Image)(bmpCrop);
}

public bool IsReusable {
    get {
        return false;
    }
}