Answered by:
resized image doesn't save

Question
-
User1280950372 posted
The image resizes fine in the following code but it doesn't save. Instead, the original-sized image is loaded back in the browser. Any suggestions?
Dim txt As TextBox Dim lbl As Label Dim rbl As RadioButtonList Dim chk As CheckBox Dim ddl As DropDownList Dim ful As FileUpload Dim lnk As HyperLink Public Function ImageResize(ByVal strSrc As String, ByVal strDest As String) As String Dim objImage As Image = Image.FromFile(strSrc) Dim dblNewHgt, dblNewWidth, dblFactor As Double Dim dblOldHgt As Double = objImage.Height Dim dblOldWidth As Double = objImage.Width Dim imgOutput As New Bitmap(objImage, dblNewWidth, dblNewHgt) Dim imgFormat = objImage.RawFormat If dblOldHgt > 300 Then dblFactor = 300 / dblOldHgt If dblFactor = 0 Then If dblOldWidth > 300 Then dblFactor = 300 / dblOldWidth End If 'compare dimensions then, depending on size, set factor for adjustment of width and height dblNewHgt = dblOldHgt dblNewWidth = dblOldWidth If dblFactor > 0 Then dblNewHgt *= dblFactor dblNewWidth *= dblFactor End If Do While dblNewHgt > 300 Or dblNewWidth > 300 dblNewHgt *= 0.9 dblNewWidth *= 0.9 Loop objImage.Dispose() objImage = Nothing ' send the resized image to the viewer imgOutput.Save(strDest, imgFormat) imgOutput.Dispose() Return strDest End Function
Saturday, August 22, 2009 5:47 AM
Answers
-
User-232462845 posted
Hi,
Don’t feel that You are abandoned..Nobody is abandoned here..
Here is the working example. It Resizes and saves the thumbnail in the application folder. and the resized image is displayed in the image control. Only change needed in the code is ..Don’t forget to change the ImageUrl value of the Image Control- Image1.
This will resize the uploaded image without loosing the aspect ratio with maximum width = 100px and maximum height = 140px (You can change these values as you like)
As it is only a trial, this example overwrites the image every time. We can save it with unique filenames also.
Try and respond.
Cheers
basheerkal
Code for Reziser.aspx page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Reziser.aspx.vb" Inherits="www_test1_Reziser" %> <%@ Import Namespace= "System.Drawing" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style34 { font-size: x-small; } .style36 { font-family: Arial, Helvetica, sans-serif; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMsg" runat="server" Font-Bold="True" Width="810px" Font-Names="Arial" Font-Size="10pt" ForeColor="Red" Height="16px"></asp:Label> <br /> <br /> <asp:Image ID="Image1" runat="server" ImageUrl="~/www/test1/TestThumb.jpg" /> <br /> <span class="style34"><span class="style36"> <asp:FileUpload ID="FileFld" runat="server" BackColor="#CFD150" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileFld" ErrorMessage="File not selected" Font-Names="Arial" Font-Size="Small"> </asp:RequiredFieldValidator> <br /> <br /> <br /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Change Photo" /> <asp:Label ID="Label6" runat="server" Font-Bold="True" Width="437px" Font-Names="Arial" Font-Size="12pt" ForeColor="Red" Height="20px" BorderStyle="None" Font-Underline="True"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Go back" PostBackUrl="cph.aspx" Visible="False" CausesValidation="False" /> </span> </span> </div> <p> </p> </form> </body> </html>
Code for resizer.aspx.vb
Imports System.Drawing Partial Class www_test1_Reziser Inherits System.Web.UI.Page Const Lx As Integer = 100 ' max width for thumbnails Const Ly As Integer = 140 ' max height for thumbnails Const upload_max_size As Integer = 150 ' max size of the upload (KB) note: this doesn't override any server upload limits Public fileExt As String ' used to store the file extension (saves finding it mulitple times) Dim newWidth, newHeight As Integer ' new width/height for the thumbnail Dim L2 As Single ' temp variable used when calculating new size Dim OriginalImg As System.Drawing.Image ' used to hold the original image Dim Msg As String ' display results Sub RessizeImage() fileExt = System.IO.Path.GetExtension(FileFld.FileName.ToLower()) If Not fileExt = ".jpg" Then Label6.Text = "File format not supported!" Label6.ForeColor = Color.FromName("Red") lblMsg.ForeColor = Color.FromName("Red") lblMsg.Text = "SORRY...You can upload only jpg files." Button1.Visible = False Exit Sub End If If FileFld.FileContent.Length > upload_max_size * 1024 Then Label6.Text = "File toolarge!" Label6.ForeColor = Color.FromName("Red") lblMsg.ForeColor = Color.FromName("Black") lblMsg.Text = "SORRY...maximum upload size permited is " & upload_max_size & "KB!." Button1.Visible = False Exit Sub End If OriginalImg = System.Drawing.Image.FromStream(FileFld.FileContent) If (OriginalImg.Width / Lx) > (OriginalImg.Height / Ly) Then L2 = OriginalImg.Width newWidth = Lx newHeight = OriginalImg.Height * (Lx / L2) If newHeight > Ly Then newWidth = newWidth * (Ly / newHeight) newHeight = Ly End If Else L2 = OriginalImg.Height newHeight = Ly newWidth = OriginalImg.Width * (Ly / L2) If newWidth > Lx Then newHeight = newHeight * (Lx / newWidth) newWidth = Lx End If End If Dim thumb As New Bitmap(newWidth, newHeight) 'Create a graphics object thumb = OriginalImg.GetThumbnailImage(newWidth, newHeight, Nothing, IntPtr.Zero) Dim gr_dest As Graphics = Graphics.FromImage(thumb) 'Re-draw the image to the specified height and width gr_dest.DrawImage(OriginalImg, 0, 0, thumb.Width, thumb.Height) ' originalimg.Save(Server.MapPath(upload_dir & upload_original & fileExt), originalimg.RawFormat) thumb.Save(Server.MapPath("TestThumb.jpg"), OriginalImg.RawFormat) Msg = "Uploaded " & FileFld.FileName & " to " & Server.MapPath("TestThumb.jpg") lblMsg.Text = Msg End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click RessizeImage() End Sub End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 30, 2009 11:00 PM
All replies
-
User-232462845 posted
D Hi
I think you have to create agraphic and re-draw the image before saving.
See the code below.i have been using it fo a long.. Original version was collected from a web article..(thanks to the author)Non-relevant lines are commnted out
(Hi, I forgot to mention one thing.. You have to import Namespace System.Drawing <%@ import Namespace="System.Drawing" %>)
Sub resizeimage() Const Lx As Integer = 100 ' max width for thumbnails Const Ly As Integer = 140 ' max height for thumbnails Const upload_dir As String = "Some path\images\" ' directory to upload file 'Const upload_original = "sample" ' filename to save original as (suffix added by script) 'Const upload_thumb = "thumb" ' filename to save thumbnail as (suffix added by script) Const upload_max_size As Integer = 50 ' max size of the upload (KB) note: this doesn't override any server upload limits Dim fileExt As String ' used to store the file extension (saves finding it mulitple times) Dim newWidth, newHeight As Integer ' new width/height for the thumbnail Dim l2 As Integer ' temp variable used when calculating new size 'Dim fileFld As HttpPostedFile ' used to grab the file upload from the form Dim originalimg As System.Drawing.Image ' used to hold the original image Dim msg As String ' display results 'FileFld is the ID of fileupload control fileExt = System.IO.Path.GetExtension(FileFld.FileName.ToLower()) If Not fileExt = ".jpg" Then 'Label6.Text = "File format not supported!" 'Label6.ForeColor = Color.FromName("Red") 'lblMsg.ForeColor = Color.FromName("Red") 'lblMsg.Text = "SORRY...You can upload only jpg files." 'Button1.Visible = False Exit Sub End If If FileFld.FileContent.Length > upload_max_size * 1024 Then 'Label6.Text = "File toolarge!" 'Label6.ForeColor = Color.FromName("Red") 'lblMsg.ForeColor = Color.FromName("Black") 'lblMsg.Text = "SORRY...maximum upload size permited is 50KB!!." 'Button1.Visible = False Exit Sub End If originalimg = System.Drawing.Image.FromStream(FileFld.FileContent) If (originalimg.Width / Lx) > (originalimg.Height / Ly) Then l2 = originalimg.Width newWidth = Lx newHeight = originalimg.Height * (Lx / l2) If newHeight > Ly Then newWidth = newWidth * (Ly / newHeight) newHeight = Ly End If Else l2 = originalimg.Height newHeight = Ly newWidth = originalimg.Width * (Ly / l2) If newWidth > Lx Then newHeight = newHeight * (Lx / newWidth) newWidth = Lx End If End If Dim thumb As New Bitmap(newWidth, newHeight) 'Create a graphics object thumb = originalimg.GetThumbnailImage(newWidth, newHeight, Nothing, IntPtr.Zero) Dim gr_dest As Graphics = Graphics.FromImage(thumb) 'Re-draw the image to the specified height and width gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height) ' originalimg.Save(Server.MapPath(upload_dir & upload_original & fileExt), originalimg.RawFormat) thumb.Save(upload_dir & "Somename" & fileExt, originalimg.RawFormat) ' msg = "Uploaded " & FileFld.FileName & " to " & upload_dir & "id" & HisId & fileExt End Sub
Saturday, August 22, 2009 9:55 AM -
User1280950372 posted
Thanks, but I'm still getting the GDI+ error (I think that's what it's called).
Saturday, August 22, 2009 2:37 PM -
User1280950372 posted
I'd like to go back to my original code and make changes from there. I'm getting an error on line 14 that reads "Parameter is not valid." I imagine it has to do with the objImage parameter but I don't know what to do about it.
Sunday, August 23, 2009 12:35 PM -
User1280950372 posted
<BUMP> Hey, folks, I still need help. Don't abandon me! Surely there's someone with expertise on problems such as this?
Friday, August 28, 2009 2:38 AM -
User-232462845 posted
Hi,
Don’t feel that You are abandoned..Nobody is abandoned here..
Here is the working example. It Resizes and saves the thumbnail in the application folder. and the resized image is displayed in the image control. Only change needed in the code is ..Don’t forget to change the ImageUrl value of the Image Control- Image1.
This will resize the uploaded image without loosing the aspect ratio with maximum width = 100px and maximum height = 140px (You can change these values as you like)
As it is only a trial, this example overwrites the image every time. We can save it with unique filenames also.
Try and respond.
Cheers
basheerkal
Code for Reziser.aspx page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Reziser.aspx.vb" Inherits="www_test1_Reziser" %> <%@ Import Namespace= "System.Drawing" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style34 { font-size: x-small; } .style36 { font-family: Arial, Helvetica, sans-serif; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMsg" runat="server" Font-Bold="True" Width="810px" Font-Names="Arial" Font-Size="10pt" ForeColor="Red" Height="16px"></asp:Label> <br /> <br /> <asp:Image ID="Image1" runat="server" ImageUrl="~/www/test1/TestThumb.jpg" /> <br /> <span class="style34"><span class="style36"> <asp:FileUpload ID="FileFld" runat="server" BackColor="#CFD150" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileFld" ErrorMessage="File not selected" Font-Names="Arial" Font-Size="Small"> </asp:RequiredFieldValidator> <br /> <br /> <br /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Change Photo" /> <asp:Label ID="Label6" runat="server" Font-Bold="True" Width="437px" Font-Names="Arial" Font-Size="12pt" ForeColor="Red" Height="20px" BorderStyle="None" Font-Underline="True"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Go back" PostBackUrl="cph.aspx" Visible="False" CausesValidation="False" /> </span> </span> </div> <p> </p> </form> </body> </html>
Code for resizer.aspx.vb
Imports System.Drawing Partial Class www_test1_Reziser Inherits System.Web.UI.Page Const Lx As Integer = 100 ' max width for thumbnails Const Ly As Integer = 140 ' max height for thumbnails Const upload_max_size As Integer = 150 ' max size of the upload (KB) note: this doesn't override any server upload limits Public fileExt As String ' used to store the file extension (saves finding it mulitple times) Dim newWidth, newHeight As Integer ' new width/height for the thumbnail Dim L2 As Single ' temp variable used when calculating new size Dim OriginalImg As System.Drawing.Image ' used to hold the original image Dim Msg As String ' display results Sub RessizeImage() fileExt = System.IO.Path.GetExtension(FileFld.FileName.ToLower()) If Not fileExt = ".jpg" Then Label6.Text = "File format not supported!" Label6.ForeColor = Color.FromName("Red") lblMsg.ForeColor = Color.FromName("Red") lblMsg.Text = "SORRY...You can upload only jpg files." Button1.Visible = False Exit Sub End If If FileFld.FileContent.Length > upload_max_size * 1024 Then Label6.Text = "File toolarge!" Label6.ForeColor = Color.FromName("Red") lblMsg.ForeColor = Color.FromName("Black") lblMsg.Text = "SORRY...maximum upload size permited is " & upload_max_size & "KB!." Button1.Visible = False Exit Sub End If OriginalImg = System.Drawing.Image.FromStream(FileFld.FileContent) If (OriginalImg.Width / Lx) > (OriginalImg.Height / Ly) Then L2 = OriginalImg.Width newWidth = Lx newHeight = OriginalImg.Height * (Lx / L2) If newHeight > Ly Then newWidth = newWidth * (Ly / newHeight) newHeight = Ly End If Else L2 = OriginalImg.Height newHeight = Ly newWidth = OriginalImg.Width * (Ly / L2) If newWidth > Lx Then newHeight = newHeight * (Lx / newWidth) newWidth = Lx End If End If Dim thumb As New Bitmap(newWidth, newHeight) 'Create a graphics object thumb = OriginalImg.GetThumbnailImage(newWidth, newHeight, Nothing, IntPtr.Zero) Dim gr_dest As Graphics = Graphics.FromImage(thumb) 'Re-draw the image to the specified height and width gr_dest.DrawImage(OriginalImg, 0, 0, thumb.Width, thumb.Height) ' originalimg.Save(Server.MapPath(upload_dir & upload_original & fileExt), originalimg.RawFormat) thumb.Save(Server.MapPath("TestThumb.jpg"), OriginalImg.RawFormat) Msg = "Uploaded " & FileFld.FileName & " to " & Server.MapPath("TestThumb.jpg") lblMsg.Text = Msg End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click RessizeImage() End Sub End Class
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 30, 2009 11:00 PM -
User1280950372 posted
Thanks, your code worked, for the most part. It sizes and displays images correctly, with the exception of one image, which turns out all black. Can't figure that one out.
Tuesday, September 1, 2009 3:39 AM -
User-232462845 posted
Hi,
Happy that it worked for you.
Which image turned out Black. What was it's original resolution. Is it a GIF?
basherkal
Tuesday, September 1, 2009 4:13 AM