创建了不同大小的拇指图像 [英] Different size thumb image created

查看:70
本文介绍了创建了不同大小的拇指图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用它创建拇指图像,但是当主图像大小不同时,拇指图像也会创建拇指图像.
我不希望创建相同大小的拇指的功能,尽管主图像大小是不同的.我尝试过日志,但无法执行.


I am using this to create thumb image, But when the main image is of different size, Then thumb images also created thumb size.
I wan''t such function that create same size thumb, though the main image size are different. I tried a log but couldn''t do it.


public static void ResizeImageFreeSize(string OriginalFile, string NewFile, int MinWidth, int MinHeight, string FileExtension)
        {
            var NewHeight = MinHeight;
            var NewWidth = MinWidth;
            var OriginalImage = System.Drawing.Image.FromFile(OriginalFile);

            if (OriginalImage.Width < MinWidth || OriginalImage.Height < MinHeight)
                throw new Exception(String.Format("Invalid Image Dimensions, please upload an image with minmum dimensions of {0}x{1}px", MinWidth.ToString(), MinHeight.ToString()));

            // If the image dimensions are the same then make the new dimensions the largest of the two mins.
            if (OriginalImage.Height == OriginalImage.Width)
                NewWidth = NewHeight = (MinWidth > MinHeight) ? MinWidth : MinHeight;
            else
            {
                if (MinWidth > MinHeight)
                    NewHeight = (int)(OriginalImage.Height * ((float)MinWidth / (float)OriginalImage.Width));
                else
                    NewWidth = (int)(OriginalImage.Width * ((float)MinHeight / (float)OriginalImage.Height));
            }

            // Just resample the Original Image into a new Bitmap
            var ResizedBitmap = new System.Drawing.Bitmap(OriginalImage, NewWidth, NewHeight);

            // Saves the new bitmap in the same format as it's source image
            FileExtension = FileExtension.ToLower().Replace(".", "");

            ImageFormat Format = null;
            switch (FileExtension)
            {
                case "jpg":
                    Format = ImageFormat.Jpeg;
                    break;
                case "gif":
                    Format = ImageFormat.Gif;
                    break;
                case "png":
                    Format = ImageFormat.Png;
                    break;
                default:
                    Format = ImageFormat.Png;
                    break;
            }

            ResizedBitmap.Save(NewFile, Format);


            // Clear handle to original file so that we can overwrite it if necessary
            OriginalImage.Dispose();
            ResizedBitmap.Dispose();
        }

推荐答案

第一个验证图像并设置新拇指图像的高度和宽度

放置(用于100x100 px缩略图)

1st validate image and set height and width of your new thumb image

put (for 100x100 px thumb image)

var ResizedBitmap = new System.Drawing.Bitmap(OriginalImage, 100, 100);





的地方




in palce of

var ResizedBitmap = new System.Drawing.Bitmap(OriginalImage, NewWidth, NewHeight);


这篇关于创建了不同大小的拇指图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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