locked
Thumbnail problem RRS feed

  • Question

  • User1139976008 posted

    Hi,

    I want to create thumbnail image for my asp.net website on the fly. Following code works for me except some images which are around 2000 X 4000 px in size. 

     

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim Originalimg As System.Drawing.Image
            Dim thumb As System.Drawing.Image
            Dim FileName As String
            Dim inp As IntPtr
            Dim width, height As Integer
            Dim width1, height1 As Integer
            Dim rootpath As String
            Dim p As Double

                Dim extn As String = ""
               
            rootpath = Server.MapPath(".")
            FileName = rootpath & "/" & Request.QueryString("FileName")
            Try
                    Originalimg = System.Drawing.Image.FromFile(FileName, True)
              


                Catch ex As Exception

                    Originalimg = System.Drawing.Image.FromFile(rootpath & "error.gif")
                    'MsgBox(ex.ToString())
                    Response.Write(ex.ToString())
                End Try

            If Not Request.QueryString("width") = Nothing Then

                    width1 = Originalimg.Width
                    height1 = Originalimg.Height

                    Dim w2 As Decimal, h2 As Decimal
                    Dim wper As Decimal, hper As Decimal, res As Decimal
                    w2 = 150
                    h2 = 110
       
                    If width1 <= w2 AndAlso height1 <= h2 Then
                        width = width1
                        height = height1
                        'it = True
                    End If
                    If width1 > w2 OrElse height1 > h2 Then
                        'If (width1 > height1) Then
                        wper = (w2 / width1) * 100
                        hper = (h2 / height1) * 100
                        If wper < hper Then
                            res = wper
                        Else
                            res = hper
                        End If
                        width = width1 * (res / 100)
                        height = height1 * (res / 100)
                           
                    End If
                           
                End If

                thumb = Originalimg.GetThumbnailImage(width, height, Nothing, inp)

                Response.ContentType = "image/jpeg"
                thumb.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

                Originalimg.Dispose()
                thumb.Dispose()
              

            End Sub

    Thursday, March 25, 2010 7:31 AM

Answers

  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 26, 2010 4:13 AM

All replies

  • User-925286913 posted

    What is the actual problem/error? 

    Thursday, March 25, 2010 9:22 AM
  • User1139976008 posted

    Hi,

    Thank you for reply. Problem is that I want to create thumbnail of image on the fly. So I used Syste.Drawing.Image.FromFile in my code. But when I pass image with large dimension like 2500 X 3000 to this code, then problem occurs. Thumbnail image doesnot get created.

    Friday, March 26, 2010 3:35 AM
  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 26, 2010 4:13 AM