Link to home
Start Free TrialLog in
Avatar of leekey
leekey

asked on

Make Thumbnail - Transparent Gif problem - Black Background

Hi,

I have the following code to create thumbnail images on the fly. It works fine with .jpg, however when a .gif is passed in as the image the background of the image
turns black.

I understand there is a problem with keeping the transparency of gif images within .net
I would be happy if the background colour was just white.

Any Ideas?


-----------------
     Public fileName As String
    Public imageWidth As Integer
    Public imageHeight As Integer


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        'get filename, width and height
        fileName = Request.QueryString("file")
        imageWidth = Request.QueryString("w")
        imageHeight = Request.QueryString("h")


        'imageWidth = 250
        'imageHeight = 400

        If imageWidth = Nothing Then
            imageWidth = 250
        Else
            imageWidth = imageWidth
        End If

        If imageHeight = Nothing Then
            imageHeight = 400
        Else
            imageHeight = imageHeight
        End If

        imageResize()

    End Sub

    Function ThumbnailCallback() As Boolean
        Return False
    End Function


    Function imageResize()

        Dim fullSizeImg, thumbNailImg As System.Drawing.Image


        'Read in the image filename to create a thumbnail of
        Dim imageUrl As String = fileName

        'Read in the width and height
        'Dim imageHeight As Integer = "200" 'Request.QueryString("h")
        'Dim imageWidth As Integer = "200" 'Request.QueryString("w")


        'Make sure that the image URL doesn't contain any /'s or \'s
        If imageUrl.IndexOf("/") >= 0 Or imageUrl.IndexOf("\") >= 0 Then
            'We found a / or \
            Response.End()
        End If

        'Add on the appropriate directory
        imageUrl = "media/" & imageUrl
        fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl))


        Dim maxWidth, MaxHeight As Integer
        ' Get width using QueryString.
        If imageWidth = Nothing Then
            maxWidth = fullSizeImg.Width
        ElseIf imageWidth = 0 Then
            maxWidth = 100
        Else
            maxWidth = imageWidth
        End If

        ' Get height using QueryString.
        If imageHeight = Nothing Then
            MaxHeight = fullSizeImg.Height
        ElseIf imageHeight = 0 Then
            MaxHeight = 100
        Else
            MaxHeight = imageHeight
        End If


        Dim imgHeight, imgWidth As Integer
        Try
            fullSizeImg = fullSizeImg.FromFile(Server.MapPath(imageUrl))
            imgHeight = fullSizeImg.Height
            imgWidth = fullSizeImg.Width

            If imgWidth > maxWidth Or imgHeight > MaxHeight Then
                'Determine what dimension is off by more
                Dim deltaWidth As Integer = imgWidth - maxWidth
                Dim deltaHeight As Integer = imgHeight - MaxHeight
                Dim scaleFactor As Double

                If deltaHeight > deltaWidth Then
                    'Scale by the height
                    scaleFactor = MaxHeight / imgHeight
                Else
                    'Scale by the Width
                    scaleFactor = maxWidth / imgWidth
                End If

                imgWidth *= scaleFactor
                imgHeight *= scaleFactor
            End If

        Catch
            fullSizeImg = fullSizeImg.FromFile(Server.MapPath("\ssm\images\ac_image\error.gif"))
        End Try


        'Do we need to create a thumbnail?
        Response.ContentType = "image/Jpeg"

        If imageHeight > 0 And imageWidth > 0 Then

            Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
            dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)


            thumbNailImg = fullSizeImg.GetThumbnailImage(imgWidth, imgHeight, dummyCallBack, IntPtr.Zero)

            ' send the resized image to the viewer  
            thumbNailImg.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg) ' output to the user
        Else
            fullSizeImg.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
        End If

        'Disposing the objects.
        thumbNailImg.Dispose()
        fullSizeImg.Dispose()

    End Function
------------------
Avatar of Ramesh Srinivas
Ramesh Srinivas
Flag of United Kingdom of Great Britain and Northern Ireland image

Here is a CreateThumbnail function that clears up GIF images and sets background to white:

Public Shared Function CreateThumbnail(ByVal lcFilename As MemoryStream, ByVal lnWidth As Integer, ByVal lnHeight As Integer) As Bitmap
        Dim bmpOut As System.Drawing.Bitmap = Nothing
        Try
            Dim loBMP As New Bitmap(lcFilename)
            Dim loFormat As ImageFormat = loBMP.RawFormat
            Dim lnRatio As Decimal
            Dim lnNewWidth As Integer = 0
            Dim lnNewHeight As Integer = 0

            '*** If the image is smaller than a thumbnail just return it
            If loBMP.Width < lnWidth And loBMP.Height < lnHeight Then
                Return loBMP
            End If

            If loBMP.Width > loBMP.Height Then
                lnRatio = CDec(lnWidth) / loBMP.Width
                lnNewWidth = lnWidth

                Dim lnTemp As Decimal = loBMP.Height * lnRatio
                lnNewHeight = CInt(lnTemp)
            Else
                lnRatio = CDec(lnHeight) / loBMP.Height
                lnNewHeight = lnHeight

                Dim lnTemp As Decimal = loBMP.Width * lnRatio
                lnNewWidth = CInt(lnTemp)
            End If

            ' System.Drawing.Image imgOut =
            '      loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
            '                              null,IntPtr.Zero);


            ' *** This code creates cleaner (though bigger) thumbnails and properly
            ' *** and handles GIF files better by generating a white background for
            ' *** transparent images (as opposed to black)
            bmpOut = New Bitmap(lnNewWidth, lnNewHeight)

            Dim g As Graphics = Graphics.FromImage(bmpOut)
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)

            loBMP.Dispose()
        Catch
            Return Nothing
        End Try

        Return bmpOut
    End Function 'CreateThumbnail
Avatar of leekey
leekey

ASKER

Hi,

Thanks for the above.

I'm trying to set this function up. However on the line below:
Public Shared Function CreateThumbnail(ByVal lcFilename As MemoryStream, ByVal lnWidth As Integer, ByVal lnHeight As Integer) As Bitmap

My visual studio is saying "MemoryStream" isn't defined.
I'm a little new to .net

Any Ideas?
Thanks
Lk.
Import the System.IO namespace.
Avatar of leekey

ASKER

OK great... done that.

This is how I'm calling the function.
But its saying (value of type "string" cannot be converted to "system.io.memorystream")

Sorry to keep coming back on this. I'm only used to basic .net functionaility....... Although I'll bet you'll say this is basic now!
--------------------------------
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        'get filename, width and height
        fileName = Request.QueryString("file")
        imageWidth = Request.QueryString("w")
        imageHeight = Request.QueryString("h")


        CreateThumbnail(fileName, imageWidth, imageHeight)

    End Sub
----------------------------------
What kind of path are you passing? Is it a URL??
Avatar of leekey

ASKER

yes.. This file is called MakeThumbnail.aspx

so I'm calling this page from others something like:
 /makethumbnail.aspx?file=logo1.gif&w=120&h=100

Lk
ASKER CERTIFIED SOLUTION
Avatar of Ramesh Srinivas
Ramesh Srinivas
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial