Beispiel #1
0
        public FileEntity UploadImageThumbnailByWidth
            (HttpPostedFile fileData, string fileFolder, string physicalpath, int width, int height, string prefix = "")
        {
            ThumnailMode mode     = ThumnailMode.EqualW;
            string       fileType = "jpg";

            FileEntity file = new FileEntity();

            if (fileData != null)
            {
                try
                {
                    string filePath = physicalpath + "/";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    file.DisplayName = Path.GetFileName(fileData.FileName);
                    file.Extension   = Path.GetExtension(file.DisplayName);
                    file.Size        = fileData.ContentLength;
                    file.ContentType = fileData.ContentType;
                    file.CreateTime  = DateTime.Now;
                    file.DbName      = GetFileDBName(file.Extension);
                    file.FilePath    = "/" + fileFolder + "/" + file.DbName;

                    Image image     = Image.FromStream(fileData.InputStream);
                    int   outWidth  = 0;
                    int   outHeight = 0;
                    ThumbnailHelper.CreateOutWAndH(image, filePath + file.DbName, width, height, mode, fileType, out outWidth, out outHeight);

                    file.ImageWidth  = outWidth;
                    file.ImageHeight = outHeight;

                    return(file);
                }
                catch (Exception ex)
                {
                    WebLogAgent.Write(ex);
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        public static void Create(Image originalImage, string outputFileName, int width, int height, ThumnailMode mode, string fileType)
        {
            Image    bitmap = null;
            Graphics g      = null;

            int towidth  = width;
            int toheight = height;

            int x  = 0;
            int y  = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
            case ThumnailMode.EqualW:
                toheight = originalImage.Height * width / originalImage.Width;
                break;

            case ThumnailMode.HW:
                break;

            case ThumnailMode.W:
                if (ow > towidth)
                {
                    toheight = originalImage.Height * width / originalImage.Width;
                }
                else
                {
                    towidth  = ow;
                    toheight = oh;
                }
                break;

            case ThumnailMode.H:
                towidth = originalImage.Width * height / originalImage.Height;
                break;

            case ThumnailMode.Cut:
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y  = 0;
                    x  = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x  = 0;
                    y  = (originalImage.Height - oh) / 2;
                }
                break;

            default:
                break;
            }

            bitmap = new Bitmap(towidth, toheight);
            g      = Graphics.FromImage(bitmap);
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode     = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

            fileType = fileType.ToLower();
            ImageFormat imageFormat = ImageFormat.Jpeg;

            if (fileType == "gif")
            {
                imageFormat = ImageFormat.Gif;
            }

            if (fileType == "bmp")
            {
                imageFormat = ImageFormat.Bmp;
            }

            if (fileType == "png")
            {
                imageFormat = ImageFormat.Png;
            }

            if (fileType == "jpg" || fileType == "jpeg")
            {
                imageFormat = ImageFormat.Jpeg;
            }

            bitmap.Save(outputFileName, imageFormat);

            if (originalImage != null)
            {
                originalImage.Dispose();
            }

            if (bitmap != null)
            {
                bitmap.Dispose();
            }

            if (g != null)
            {
                g.Dispose();
            }
        }
Beispiel #3
0
        public static void Create(string inputFileName, string outputFileName, int width, int height, ThumnailMode mode, string fileType)
        {
            Image originalImage = Image.FromFile(inputFileName);

            Create(originalImage, outputFileName, width, height, mode, fileType);
        }