Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPEG colors being mangled

Status
Not open for further replies.

instar4per

Programmer
Jan 21, 2002
48
US
Despite this being a web application, I am asking here, because the core of it is done in VB.

I have an ASP script which accepts an uploaded image ( using the FreeASPUpload class, ), and passes the temporary file's name to a custom component, which is supposed to do the following:

Load the image into an IPicture variable, using LoadPicture.
Convert the image to a DIB and resize.
Use the Intel JPEG library and save the image as a temporary JPEG.
Pass the temporary name back to the ASP script
Open the temporary file as an ADODB Stream, and insert it into a database.
Eventually, pull the data from the database, and pass it as a JPEG file back to the client.

I had all of this working. I modified the math on my resizing code, and suddenly it seems as though the component is now producing images that are no greater than 256 colors. See:

The DIB and JPEG code I am using comes from a project on Planet Source Code, -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is my full class code:

Option Explicit

Dim tmpFile As String
Dim mImage As IPicture
Dim DIB As cDIBSection

Private mPath As String

Public Property Let Path(New_Path As String)

Dim i As Long

If Right(New_Path, 1) <> &quot;\&quot; Then New_Path = New_Path & &quot;\&quot;

Randomize
For i = 1 To 20
tmpFile = tmpFile & Chr(65 + Rnd() * 23)
Next
tmpFile = New_Path & tmpFile & &quot;.jpg&quot;

End Property

Public Function LoadImage(sFile) As Boolean

LoadImage = True

On Error Resume Next
Set mImage = LoadPicture(sFile)
If Err.Number Then
LoadImage = False
Exit Function
End If

Set DIB = New cDIBSection

Kill sFile

End Function

Public Function Picture() As IPicture

Set Picture = mImage

End Function

Public Function MakeThumb()

Dim fAspect As Single
Dim bTaller As Boolean
Dim Quality As Long

Dim Height As Long
Dim Width As Long
Dim nHeight As Long
Dim nWidth As Long

DIB.CreateFromPicture mImage

Height = DIB.Height
Width = DIB.Width

If Width > Height Then
fAspect = Height / Width
If Height > 160 Or Width > 120 Then
nWidth = 160
nHeight = 160 * fAspect
End If
Else
fAspect = Width / Height
If Height > 160 Or Width > 120 Then
nWidth = 120 * fAspect
nHeight = 120
End If
End If

Set DIB = DIB.Resample(nHeight, nWidth)

Quality = 30

Do
SaveJPG DIB, tmpFile, Quality
Quality = Quality + 15
'Debug.Print FileLen(tmpFile)
Loop While (FileLen(tmpFile) < 5000) And (Quality < 100)

MakeThumb = tmpFile

End Function

Public Function MakeImage()

Dim Quality As Long
Dim i As Long

DIB.CreateFromPicture mImage

Quality = 100

SaveJPG DIB, tmpFile, Quality

While (FileLen(tmpFile) > 500000)
'Debug.Print FileLen(tmpFile)
SaveJPG DIB, tmpFile, Quality
Quality = Quality - 10
Wend

MakeImage = tmpFile

End Function

Private Sub Class_Terminate()

Set DIB = Nothing
Set mImage = Nothing
On Error Resume Next
Kill tmpFile
On Error GoTo 0

End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And here is how it's used:

For Each f In Upload.Files
Recordset.AddNew
If Not Image.LoadImage(Server.MapPath(f.FileName)) Then
Response.Write(&quot;Can't load image&quot;)
Response.End
End If
Stream.Open
Stream.Type = 1

Stream.LoadFromFile Image.MakeImage
Recordset(&quot;Image&quot;) = Stream.Read

Stream.LoadFromFile Image.MakeThumb
Recordset(&quot;Thumbnail&quot;) = Stream.Read

Recordset(&quot;ImageID&quot;) = Right(&quot;000&quot; & Hex(CLng(ImgCount)), 3)
If Not (Request.QueryString(&quot;which&quot;) = &quot;private&quot;) Then
Recordset(&quot;UserID&quot;) = &quot;0&quot;
Else
Recordset(&quot;UserID&quot;) = Session(&quot;ID&quot;)
Recordset(&quot;Name&quot;) = Left(Upload.Form(&quot;name&quot;), 250)
Recordset(&quot;Description&quot;) = Upload.Form(&quot;comment&quot;)
End If
Recordset.Update
Next
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is how the images are regurgitated:
<!-- #include virtual = &quot;common/includes.asp&quot; -->
<%
Recordset.Open &quot;SELECT Images.Thumbnail, Images.Image FROM Images WHERE Images.ID = &quot; & Request.QueryString(&quot;id&quot;)
Response.ContentType = &quot;image/jpeg&quot;
Response.Clear()
If Request.QueryString(&quot;image&quot;) = &quot;full&quot; Then
Response.BinaryWrite(Recordset(&quot;Image&quot;))
Else
Response.BinaryWrite(Recordset(&quot;Thumbnail&quot;))
End If
Response.Flush()
Response.End()
Recordset.Close
%>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I know that's a lot to digest at once. I really am at my wits end here, guys. If I add this project to VB, then step through it while displaying the results in a picture box, then I see no abnormalities. Everything works beautifully. Yet as soon as I use it in IIS, I get the link I posted above.

Any ideas? Thanks, guys.

-iNSTA
aim: instar4per
email: instar4per @ hotmail.com
 
Actually it's quite simple.
I have the same problem, and after searing for a while i found out what the problem is.

When the iis runs, it runs in the backgroubd. No network shares, no desktop, no printers. Wait no desktop ????

Yep the iis runs in minimal mode. No more then 255 colors. And since you use an image box that image box can contain no more than 255 colors.

Solution: None..... Try decoding the image the hard way.
I tried to read the image ans skip pixels. That did not work.

I'm now working on a client side solution. Activex running on the browser (like microsoft update) However i cant read any enviroment variabels, or set vars in the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top