Hello all,
I found some code that resizes an uploaded image ( stream ) to a pre-determined size.
Using this code, it works perfectly..
What I'd like to do is move this code into a Class so I can call it from other pages, so I created a class and added this code...Code:' make sure we have a file . If FileUpload3.HasFile Then Dim name As String = FileUpload3.PostedFile.FileName Dim Justname As String = Path.GetFileName(name) Dim length As Integer = FileUpload3.PostedFile.ContentLength ' Response.Write("<hr>name:" & Justname & "<hr>") ' Response.Write("<hr>Len:" & length & "<hr>") Dim imageBytes As Byte() = New Byte(length - 1) {} Dim imageStream As Stream = FileUpload3.PostedFile.InputStream ' imageStream.Read(imageBytes, 0, length) ' Context.Response.BinaryWrite(ImgResizeer(imageStream, 200)) Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(imageStream) Dim Width1 As Integer = Bitmap1.Width Dim Height1 As Integer = Bitmap1.Height ' define the width for this file. If Width1 > 250 Then Dim Width2 As Integer = 250 Dim Height2 As Integer = Height1 * Width2 / Width1 Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2) Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2) Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2) End Using Using MemoryStream2 As New System.IO.MemoryStream Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png) Context.Response.BinaryWrite(MemoryStream2.ToArray) End Using End Using End If End Using End If
As you can see, I'm trying to pass in a stream and defined width, and I want it to pass back the image ( if the size if greater than what I expected.Code:Imports Microsoft.VisualBasic Imports System.IO Imports System Imports System.Xml Imports System.Xml.Xsl Imports System.Data.SqlClient Imports System.DirectoryServices Imports System.Data Imports System.Drawing Public Class ImageStreamResize Public Function ImgResizeer(imageStream As Stream, requiredWidth As Integer) As Stream Dim finalStr As Stream = imageStream Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(imageStream) Dim Width1 As Integer = Bitmap1.Width Dim Height1 As Integer = Bitmap1.Height ' define the width for this file. If Width1 > requiredWidth Then Dim Width2 As Integer = requiredWidth Dim Height2 As Integer = Height1 * Width2 / Width1 Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2) Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2) Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2) End Using Using MemoryStream2 As New System.IO.MemoryStream Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png) finalStr = MemoryStream2 End Using End Using End If End Using Return finalStr End Function End Class
I'm trying to call the class like so.
Don't worry about the cc.imgStr this is just the path to my class which is defined futher up.Code:' make sure we have a file and that we have an ID to set it to. If FileUpload3.HasFile Then Dim name As String = FileUpload3.PostedFile.FileName Dim Justname As String = Path.GetFileName(name) Dim length As Integer = FileUpload3.PostedFile.ContentLength ' Response.Write("<hr>name:" & Justname & "<hr>") ' Response.Write("<hr>Len:" & length & "<hr>") Dim imageBytes As Byte() = New Byte(length - 1) {} Dim imageStream As Stream = FileUpload3.PostedFile.InputStream ' imageStream.Read(imageBytes, 0, length) Context.Response.BinaryWrite(cc.imgStr.ImgResizeer(imageStream, 200)) End If
The Error I get is "Value of type system.io.stream, cannot be converted to ' 1 dimensional array of byte' "
Any help would be most appreciated!
Many thanks
Dave


Reply With Quote
