示例#1
0
    public static byte[] GetThumbnailImage(string fileName, byte[] imageBytes, int size)
    {
        if (size <= 0)
        {
            throw new Exception("ErrorCode : 1390/10/23-22:04 : " + QueryStringCoder.Code("اندازه درخواستي جهت تامبنيل تصوير(" + size + ") مجاز نمي باشد!"));
        }
        ImageFormat  imageType = ConvertFileExtensionToImageFormat(GetFileType(fileName));
        MemoryStream ms        = new MemoryStream(imageBytes);

        ms.Position = 0;
        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
        int width;
        int height;

        if (img.Height > img.Width)
        {
            height = size.ToInt();
            width  = img.Width * size.ToInt() / img.Height;
        }
        else     //if (img.height <= img.width)
        {
            width  = size.ToInt();
            height = img.Height * size.ToInt() / img.Width;
        }
        if (imageType == ImageFormat.Png || imageType == ImageFormat.Gif)
        {
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            img       = img.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
            imageType = ImageFormat.Png;
        }
        else
        {
            img = ImageResizer.ConstrainProportions(img, height, Dimensions.Height);
        }
        MemoryStream thumbnailImageStream = new MemoryStream();

        thumbnailImageStream.Position = 0;
        img.Save(thumbnailImageStream, imageType);
        return(thumbnailImageStream.ToArray());
    }