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.FromF ile(Server .MapPath(i mageUrl))
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(Serve r.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(Serve r.MapPath( "\ssm\imag es\ac_imag e\error.gi f"))
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.GetTh umbnailIma geAbort
dummyCallBack = New System.Drawing.Image.GetTh umbnailIma geAbort(Ad dressOf ThumbnailCallback)
thumbNailImg = fullSizeImg.GetThumbnailIm age(imgWid th, imgHeight, dummyCallBack, IntPtr.Zero)
' send the resized image to the viewer
thumbNailImg.Save(Response .OutputStr eam, Imaging.ImageFormat.Jpeg) ' output to the user
Else
fullSizeImg.Save(Response. OutputStre am, Imaging.ImageFormat.Jpeg)
End If
'Disposing the objects.
thumbNailImg.Dispose()
fullSizeImg.Dispose()
End Function
------------------
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.FromF
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(Serve
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(Serve
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.GetTh
dummyCallBack = New System.Drawing.Image.GetTh
thumbNailImg = fullSizeImg.GetThumbnailIm
' send the resized image to the viewer
thumbNailImg.Save(Response
Else
fullSizeImg.Save(Response.
End If
'Disposing the objects.
thumbNailImg.Dispose()
fullSizeImg.Dispose()
End Function
------------------
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.
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.
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
-------------------------- --------
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??
ASKER
yes.. This file is called MakeThumbnail.aspx
so I'm calling this page from others something like:
/makethumbnail.aspx?file=l ogo1.gif&w =120&h=100
Lk
so I'm calling this page from others something like:
/makethumbnail.aspx?file=l
Lk
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(ln
' 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.I
g.FillRectangle(Brushes.Wh
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)
loBMP.Dispose()
Catch
Return Nothing
End Try
Return bmpOut
End Function 'CreateThumbnail