Har ett jätte probelm. Har skrivit en funktion som skalar om bilder till ett värde (högsta bredd, höjd). Fungerar kalas bra på vissa bilder men vissa bilder blir helt sönder rastade (ser ut som pixlarna är dubbelt så stora).
Kan nån hjälpa mig och kolla koden. Har kollat på nätet och testat masssor med saker men får inte rätt på det.
Koden hämtar in bilden från en databas. Och skapar sedan en bild..
Hjälp
[KOD]
Public Function CreateHTTPImage(ByVal sqlvalue As Guid, ByVal imageWidth As Int32, ByVal imageHeight As Int32, ByVal path As String) As Boolean
Dim sql As New SqlClient.SqlCommand
sql.CommandText = "SELECT fileBytes, fileData FROM [dbo].[fileserver.data] WHERE fileId = '" & sqlvalue.ToString & "'"
sql.CommandType = CommandType.Text
Dim sqlreader As SqlClient.SqlDataReader = db.DataRead(sql)
sqlreader.Read()
Dim _image As System.Drawing.Image
_image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(CType(sqlreader("fileData"), Byte()), 0, CInt(sqlreader("fileBytes"))))
Dim width As Int32 = imageWidth
Dim height As Int32 = imageHeight
Dim imgHeight As Integer = _image.Height
Dim imgWidth As Integer = _image.Width
If imgWidth > width Or imgHeight > height Then
'Determine what dimension is off by more
Dim deltaWidth As Integer = imgWidth - width
Dim deltaHeight As Integer = imgHeight - width
Dim scaleFactor As Double
If deltaHeight > deltaWidth Then
'Scale by the height
scaleFactor = width / imgHeight
Else
'Scale by the Width
scaleFactor = width / imgWidth
End If
imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If
' Skriv till text till bilden.
Dim bmap As System.Drawing.Image
bmap = System.Drawing.Image.FromStream(New System.IO.MemoryStream(CType(sqlreader("fileData"), Byte()), 0, CInt(sqlreader("fileBytes"))))
Dim _newimage As System.Drawing.Image
_newimage = _image.GetThumbnailImage(imgWidth, imgHeight, Nothing, System.IntPtr.Zero)
Dim _path As String = "C:\fserver\" + path + sqlvalue.ToString + ".jpg"
Dim g As Graphics = Graphics.FromImage(_newimage)
g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
g.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
g.CompositingMode = Drawing.Drawing2D.CompositingMode.SourceCopy
_newimage.Save(_path)
End Function
[/KOD]
Kan nån hjälpa mig och kolla koden. Har kollat på nätet och testat masssor med saker men får inte rätt på det.
Koden hämtar in bilden från en databas. Och skapar sedan en bild..
Hjälp
[KOD]
Public Function CreateHTTPImage(ByVal sqlvalue As Guid, ByVal imageWidth As Int32, ByVal imageHeight As Int32, ByVal path As String) As Boolean
Dim sql As New SqlClient.SqlCommand
sql.CommandText = "SELECT fileBytes, fileData FROM [dbo].[fileserver.data] WHERE fileId = '" & sqlvalue.ToString & "'"
sql.CommandType = CommandType.Text
Dim sqlreader As SqlClient.SqlDataReader = db.DataRead(sql)
sqlreader.Read()
Dim _image As System.Drawing.Image
_image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(CType(sqlreader("fileData"), Byte()), 0, CInt(sqlreader("fileBytes"))))
Dim width As Int32 = imageWidth
Dim height As Int32 = imageHeight
Dim imgHeight As Integer = _image.Height
Dim imgWidth As Integer = _image.Width
If imgWidth > width Or imgHeight > height Then
'Determine what dimension is off by more
Dim deltaWidth As Integer = imgWidth - width
Dim deltaHeight As Integer = imgHeight - width
Dim scaleFactor As Double
If deltaHeight > deltaWidth Then
'Scale by the height
scaleFactor = width / imgHeight
Else
'Scale by the Width
scaleFactor = width / imgWidth
End If
imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If
' Skriv till text till bilden.
Dim bmap As System.Drawing.Image
bmap = System.Drawing.Image.FromStream(New System.IO.MemoryStream(CType(sqlreader("fileData"), Byte()), 0, CInt(sqlreader("fileBytes"))))
Dim _newimage As System.Drawing.Image
_newimage = _image.GetThumbnailImage(imgWidth, imgHeight, Nothing, System.IntPtr.Zero)
Dim _path As String = "C:\fserver\" + path + sqlvalue.ToString + ".jpg"
Dim g As Graphics = Graphics.FromImage(_newimage)
g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
g.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
g.CompositingMode = Drawing.Drawing2D.CompositingMode.SourceCopy
_newimage.Save(_path)
End Function
[/KOD]
Comment