Question : Re size Image in memory from Byte()

Hi to all,

I have an SQL 2005 Database. I store my images in the database as an Image. I want to re size the image to a thumb nail and then save that thumbnail to the database in a different table. Using the code below I keep on getting an error if I want to view the image again.

What am I doing wrong and is this the right way of resizing the image in memory?

Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
Public Function createThumbandFull(ByVal ItemCode As String) As AUErrorCapture Implements IAUDBInUpDel.createThumbandFull
        Dim errorReturn As AUErrorCapture = Nothing

        Try

            Dim db As New databaseLinqDataContext
            db.Connection.ConnectionString =
                furConn.ConnectionString

            Dim itemIm = From i In db.AUOrigImages _
                         Where i.ItemCode = ItemCode _
                         Select i.ImageContent

            Dim newResizedImage As Byte() = Nothing
            Dim stream As MemoryStream

            For Each retI In itemIm
                newResizedImage = retI.ToArray
                stream = New MemoryStream(newResizedImage)
                ResizeThumbImage(CDbl(0.1), stream, ItemCode)
             Next

        Catch ex As Exception
            Throw ex
        End Try

        Return errorReturn
    End Function





Private Sub ResizeThumbImage(ByVal scaleFactor As Double, ByVal Filetoresize As MemoryStream, ByVal itemCodetoSave As String)

        Try

            Dim image__1 = System.Drawing.Image.FromStream(Filetoresize)
            Dim newWidth = CInt((image__1.Width * scaleFactor))
            Dim newHeight = CInt((image__1.Height * scaleFactor))
            Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)

            Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic

            Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
            thumbnailGraph.DrawImage(image__1, imageRectangle)
            'Encoding Parameters
            Dim quality As Integer = 90 '90 is the magic setting - really. It has excellent quality and file size.
            Dim encoderParameters As New System.Drawing.Imaging.EncoderParameters(1)
            encoderParameters.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CLng(quality))

            Dim fs As System.IO.MemoryStream = New System.IO.MemoryStream()

            thumbnailBitmap.Save(fs, image__1.RawFormat)

            Dim img As Byte() = New Byte(fs.Length) {}


            Dim db As New databaseLinqDataContext
            db.Connection.ConnectionString =
                furConn.ConnectionString

            db.AUResizeThumbImage(itemCodetoSave, itemCodetoSave & ".jpg", img)

            thumbnailGraph.Dispose()
            thumbnailBitmap.Dispose()
            image__1.Dispose()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Answer : Re size Image in memory from Byte()

Almost right, see in the ResizeThumbImage function:

thumbnailBitmap.Save(fs, image__1.RawFormat)
Dim img As Byte() = New Byte(fs.Length) {}
db.AUResizeThumbImage(itemCodetoSave, itemCodetoSave & ".jpg", img)


You're never actually putting any content into the img byte array.  You're declaring it the right size, but its still empty.

Change this line:
   Dim img As Byte() = New Byte(fs.Length) {}
to:
   Dim img as Byte() = fs.ToArray()
Random Solutions  
 
programming4us programming4us