I have code which takes an image, uploaded from a web page, and saves it to
a database. Now I want to always resize an uploaded image before it is
saved to the database. My code to resize is below and of course it's not
working properly because my web page which displays the resulting image from
the database is not showing the image. I **think** the problem is in my
converting the bitmap back to a byte array. If I change my code back to
saving the image as originally uploaded the image displays fine.
Brad
'===============================
' Upload conversion code
Public Sub SaveUploadedImage(ByVal itemId As Integer, ByVal imageFile As
System.Web.HttpPostedFile)
Dim contentSize As Integer
Dim contentType As String = imageFile.ContentType
'================================================= ====
Dim origImage As Bitmap =
CType(System.Drawing.Image.FromStream(imageFile.In putStream), Bitmap)
Dim maxHeight As Integer = 340
Dim maxWidth As Integer = 170
Dim origWidth As Integer = origImage.Width
Dim origHeight As Integer = origImage.Height
Dim newWidth As Integer = origWidth
Dim newHeight As Integer = origHeight
If ((origWidth > maxWidth) And (maxWidth > 0)) Or ((origHeight > maxHeight)
And (maxHeight > 0)) Then
Dim factor As Double = 1
If (maxWidth > 0) Then
factor = Math.Min(factor, (maxWidth / origWidth))
End If
If (maxHeight > 0) Then
factor = Math.Min(factor, (maxHeight / origHeight))
End If
newWidth = CInt(origWidth * factor)
newHeight = CInt(origHeight * factor)
End If
Dim newImage As Bitmap = New Bitmap(newWidth, newHeight)
Dim gfx As Graphics = Graphics.FromImage(newImage)
With gfx
.CompositingMode = Drawing2D.CompositingMode.SourceOver
.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
.DrawImage(origImage, 0, 0, newWidth, newHeight)
.Dispose()
End With
'==========================================
' Is problem in this section of code????
'==========================================
Dim objStream As New MemoryStream
If contentType.IndexOf("jpeg") >= 0 Then
newImage.Save(objStream, Imaging.ImageFormat.Jpeg)
Else
newImage.Save(objStream, Imaging.ImageFormat.Gif)
End If
newImage.Dispose()
contentSize = CType(objStream.Length, Integer)
Dim imageData(contentSize) As Byte
objStream.Read(imageData, 0, contentSize)
'==========================================
' set sql sp parameters
With dataObj
Dim parameterContent As SqlParameter = New SqlParameter("@Content",
SqlDbType.Image)
parameterContent.Value = imageData
.Parameters.Add(parameterContent)
Dim parameterContentSize As SqlParameter = New
SqlParameter("@ContentSize", SqlDbType.Int, 4)
parameterContentSize.Value = contentSize
.Parameters.Add(parameterContentSize)
Dim parameterContentType As SqlParameter = New
SqlParameter("@ContentType", SqlDbType.VarChar, 25)
parameterContentType.Value = contentType
.Parameters.Add(parameterContentType)
End With
End Sub
******************************************
FYI - Here's excerpt of code from the web page which displays the image
Dim content As Byte()
Dim byteSize As Integer
Dim contentType As String
Dim dr As SqlDataReader = dataObj.GetImage(itemId)
If dr.Read Then
byteSize = Ctype(dr("contentsize"),integer)
If byteSize > 0 Then
content = CType(dr("content"), Byte())
contentType = dr("contentType").ToString
End If
End If
dr.Close()
If byteSize > 0 Then
Response.ContentType = contentType
Response.OutputStream.Write(content, 0, byteSize)
End If 2 3960
Hi Brad,
You may try to change the code as below. contentSize = CType(objStream.Length, Integer) Dim imageData(contentSize) As Byte objStream.Read(imageData, 0, contentSize)
To
contentSize = CType(objStream.Length, Integer)
objStream.Position =0 'This is the key point
Dim imageData(contentSize-1) As Byte
objStream.Read(imageData, 0, contentSize)
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
It works. Thank you very much.
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:Kl**************@cpmsftngxa06.phx.gbl... Hi Brad,
You may try to change the code as below.
contentSize = CType(objStream.Length, Integer) Dim imageData(contentSize) As Byte objStream.Read(imageData, 0, contentSize) To
contentSize = CType(objStream.Length, Integer) objStream.Position =0 'This is the key point Dim imageData(contentSize-1) As Byte objStream.Read(imageData, 0, contentSize)
Best regards,
Peter Huang Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no
rights. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mat |
last post by:
Hi,
I have a system that uploads images as BLOBs in a database. I also
have a function that I use to resize uploaded images before saving as
files. I would like to combine these two by resising...
|
by: Jim |
last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick)
is a processor intensive exercise. I'm setting up a site where users
will be uploading up to 10 images along with the details...
|
by: Satish Appasani |
last post by:
Hi:
I have a ASP.NET form with Web layout which I've achieved using panels. In
one of the tab I have a File control to upload Images. When I put a file in
the file control and move to another...
|
by: Ron Vecchi |
last post by:
I am using asp.net to upload an image and then perform resizing on it and
saving the different sizes to file.
The resized images were coming up and being displayed in the bowser fine but
the image...
|
by: Tarren |
last post by:
Hi there:
I have figured out how to take the uploaded file from the form input and
save it into the database, but I am wondering (and looking for examples)
how can I stream the image back to the...
|
by: IkBenHet |
last post by:
Hello,
I use this script to upload image files to a folder on a IIS6 server:
******************* START UPLOAD.ASPX FILE **********************
<%@ Page Language="VB" Debug="true" %>
|
by: Blasting Cap |
last post by:
I would like to try to see if an image uploaded to a SQL database is
over a specific size, and if so, to resize it when it is displayed. The
following is in the Page_load event.
Dim strImageID...
|
by: mishrarajesh44 |
last post by:
hii all,
I am facing a problem currently..
i have a script for image uploading and resizing..
the image uploading takes place properly for every size images..
but, the resizing works for...
|
by: zizi2 |
last post by:
Hi,
I have a webform that uload images as well. The image is uploaded well but the problem arises when the user does not upload any image. I want the form to save nothing for the image_name in the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |