|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Upload image and then resize it - A generic error occurred in GDI+.I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what I can tell, no one really knows what it means.... Here's my public function Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As Integer) As String Dim img As System.Drawing.Image img = System.Drawing.Image.FromFile(p) Dim iw As Integer = img.Width Dim ih As Integer = img.Height Dim nw, nh As Integer Dim per As Decimal If iw > w Or ih > h Then 'check to see if resize is necessary If w > h Then 'get the larger dimension and get percentage per = iw / w Else per = ih / h End If 'create new sizes based on percentages nw = iw * per nh = ih * per 'now save it Dim size As New Size(nw, nh) Dim nimg As New Bitmap(img, size) nimg.Save(p) lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & nw & "x" & nh ElselblTemp.Text = "No resize necessary." End If End Function and here is my code for the upload, abbreviated If picType = "image/jpeg" Or picType = "image/gif" Or picType = "image/pjpeg" Or picType = "image/bmp" Then File.PostedFile.SaveAs(path) ResizeImage(path, 120, 95) lblError.Text = "The speaker photo uploaded sucessfully! Make sure to click Update below to save changes." Else lblError.Text = "Invalid image format. Only JPG, JPEG, GIF and BMP images are allowed." End If Again, the upload works great Thanks!! -- David Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com Besides the point, I just realized my math is wrong, please don't critique. I just need to resolve the resize issue.
-- David Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders!Howdy, Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what I can tell, no one really knows what it means.... Here's my public function Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As Integer) As String Dim img As System.Drawing.Image img = System.Drawing.Image.FromFile(p) Dim iw As Integer = img.Width Dim ih As Integer = img.Height Dim nw, nh As Integer Dim per As Decimal If iw > w Or ih > h Then 'check to see if resize is necessary If w > h Then 'get the larger dimension and get percentage per = iw / w Else per = ih / h End If 'create new sizes based on percentages nw = iw * per nh = ih * per 'now save it Dim size As New Size(nw, nh) Dim nimg As New Bitmap(img, size) nimg.Save(p) lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & nw & "x" & nh ElselblTemp.Text = "No resize necessary." End If End Function and here is my code for the upload, abbreviated If picType = "image/jpeg" Or picType = "image/gif" Or picType = "image/pjpeg" Or picType = "image/bmp" Then File.PostedFile.SaveAs(path) ResizeImage(path, 120, 95) lblError.Text = "The speaker photo uploaded sucessfully! Make sure to click Update below to save changes." Else lblError.Text = "Invalid image format. Only JPG, JPEG, GIF and BMP images are allowed." End If Again, the upload works great Thanks!! -- David Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com Hi David,
We have reviewed this issue and are currently researching on it. We will update you ASAP. Thanks for your patience! Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights." Hey David,
So this seems a pure GDI+ image processing issue. Does this problem occurs only when the uploaded image is of certain format or is a common issue that will occur for any images that uploaded onto the server? Generally I think we can throubleshoot through the following steps: 1. Make sure that the image is uploaded correctly onto the server , open it in image viewer to make sure the data is not corrupted. 2. Then, use some standard image resizeing code to resize the image... here is some simple GDI+ code I picked from net which resize the image through percentage value: static Image ScaleByPercent(Image imgPhoto, int Percent) { float nPercent = ((float)Percent/100); int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX,destY,destWidth,destHeight), new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } You can try resizing through the code also to see whether it can work correctly.... If there're any other finding, please feel free to post here. Thanks, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) -------------------- Show quote | From: "David Lozzi" <DavidLozzi@nospam.nospam> microsoft.public.dotnet.framework.aspnet:363864| References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> | Subject: Re: Upload image and then resize it - A generic error occurred in GDI+. | Date: Fri, 9 Dec 2005 11:15:17 -0500 | Lines: 270 | MIME-Version: 1.0 | Content-Type: multipart/alternative; | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | Newsgroups: microsoft.public.dotnet.framework.aspnet | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet critique. I just need to resolve the resize issue.| | Besides the point, I just realized my math is wrong, please don't | -- .Nets built in upload, so much easier than 3rd party uploaders!| David Lozzi | Web Applications Developer | dlozzi@(remove-this)delphi-ts.com | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... | Howdy, | I have a function that uploads an image and that works great. I love | Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what I can tell, no one really knows what it means.... Here's my public function | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As Integer) As StringShow quote | Dim img As System.Drawing.Image & "x" & nh| img = System.Drawing.Image.FromFile(p) | Dim iw As Integer = img.Width | Dim ih As Integer = img.Height | Dim nw, nh As Integer | Dim per As Decimal | If iw > w Or ih > h Then 'check to see if resize is necessary | If w > h Then 'get the larger dimension and get percentage | per = iw / w | Else | per = ih / h | End If | 'create new sizes based on percentages | nw = iw * per | nh = ih * per | 'now save it | Dim size As New Size(nw, nh) | Dim nimg As New Bitmap(img, size) | nimg.Save(p) | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & nw | Else picType = "image/pjpeg" Or picType = "image/bmp" Then| lblTemp.Text = "No resize necessary." | End If | End Function | and here is my code for the upload, abbreviated | If picType = "image/jpeg" Or picType = "image/gif" Or | File.PostedFile.SaveAs(path) sucessfully! Make sure to click Update below to save changes."| ResizeImage(path, 120, 95) | lblError.Text = "The speaker photo uploaded | Else JPEG, GIF and BMP images are allowed."| lblError.Text = "Invalid image format. Only JPG, Show quote | End If | Again, the upload works great | Thanks!! | -- | David Lozzi | Web Applications Developer | dlozzi@(remove-this)delphi-ts.com | | I've tried similar to your code. I'm writing in VB so I translated from C to
VB, that might be where its messing up, somewhere in translation. Of course I don't have the original translation anymore, but I will work on translating your and see how I do. Thanks, -- Show quoteDavid Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... > Hey David, > > So this seems a pure GDI+ image processing issue. Does this problem occurs > only when the uploaded image is of certain format or is a common issue > that > will occur for any images that uploaded onto the server? Generally I think > we can throubleshoot through the following steps: > > 1. Make sure that the image is uploaded correctly onto the server , open > it > in image viewer to make sure the data is not corrupted. > > 2. Then, use some standard image resizeing code to resize the image... > here is some simple GDI+ code I picked from net which resize the image > through percentage value: > > static Image ScaleByPercent(Image imgPhoto, int Percent) > { > float nPercent = ((float)Percent/100); > > int sourceWidth = imgPhoto.Width; > int sourceHeight = imgPhoto.Height; > int sourceX = 0; > int sourceY = 0; > > int destX = 0; > int destY = 0; > int destWidth = (int)(sourceWidth * nPercent); > int destHeight = (int)(sourceHeight * nPercent); > > Bitmap bmPhoto = new Bitmap(destWidth, destHeight, > PixelFormat.Format24bppRgb); > bmPhoto.SetResolution(imgPhoto.HorizontalResolution, > imgPhoto.VerticalResolution); > > Graphics grPhoto = Graphics.FromImage(bmPhoto); > grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; > > grPhoto.DrawImage(imgPhoto, > new Rectangle(destX,destY,destWidth,destHeight), > new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), > GraphicsUnit.Pixel); > > grPhoto.Dispose(); > return bmPhoto; > } > > You can try resizing through the code also to see whether it can work > correctly.... > > If there're any other finding, please feel free to post here. > > Thanks, > > Steven Cheng > Microsoft Online Support > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > > > -------------------- > | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > | Subject: Re: Upload image and then resize it - A generic error occurred > in GDI+. > | Date: Fri, 9 Dec 2005 11:15:17 -0500 > | Lines: 270 > | MIME-Version: 1.0 > | Content-Type: multipart/alternative; > | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" > | X-Priority: 3 > | X-MSMail-Priority: Normal > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > | Newsgroups: microsoft.public.dotnet.framework.aspnet > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl > | Xref: TK2MSFTNGXA02.phx.gbl > microsoft.public.dotnet.framework.aspnet:363864 > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | > | Besides the point, I just realized my math is wrong, please don't > critique. I just need to resolve the resize issue. > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... > | Howdy, > | I have a function that uploads an image and that works great. I love > Nets built in upload, so much easier than 3rd party uploaders! > | Now I am making a public function that will take the path of the > uploaded image, and resize it with the provided dimensions. My function is > below. The current function is returning an error when run from the upload > function: A generic error occurred in GDI+. Not sure what exactly that > means. From what I can tell, no one really knows what it means.... Here's > my public function > | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal > h > As Integer) As String > | Dim img As System.Drawing.Image > | img = System.Drawing.Image.FromFile(p) > | Dim iw As Integer = img.Width > | Dim ih As Integer = img.Height > | Dim nw, nh As Integer > | Dim per As Decimal > | If iw > w Or ih > h Then 'check to see if resize is necessary > | If w > h Then 'get the larger dimension and get percentage > | per = iw / w > | Else > | per = ih / h > | End If > | 'create new sizes based on percentages > | nw = iw * per > | nh = ih * per > | 'now save it > | Dim size As New Size(nw, nh) > | Dim nimg As New Bitmap(img, size) > | nimg.Save(p) > | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & > nw > & "x" & nh > | Else > | lblTemp.Text = "No resize necessary." > | End If > | End Function > | and here is my code for the upload, abbreviated > | If picType = "image/jpeg" Or picType = "image/gif" Or > picType = "image/pjpeg" Or picType = "image/bmp" Then > | File.PostedFile.SaveAs(path) > | ResizeImage(path, 120, 95) > | lblError.Text = "The speaker photo uploaded > sucessfully! Make sure to click Update below to save changes." > | Else > | lblError.Text = "Invalid image format. Only JPG, > JPEG, GIF and BMP images are allowed." > | End If > | Again, the upload works great > | Thanks!! > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | > | > After reviewing your code, this function returns the resized image as an
image to the calling script. I need the resized image to be saved to disk. My script is below. I get an error: Object reference not set to an instance of an object, which pertains to the graphic object because when I remove that chunk of code, I get the same GDI+ error. Thanks!! Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As Integer) As String Dim img As System.Drawing.Image img = System.Drawing.Image.FromFile(p) Dim iw As Integer = img.Width Dim ih As Integer = img.Height Dim nw, nh As Integer Dim per As Decimal If iw > w Or ih > h Then 'check to see if resize is necessary If iw > ih Then 'get the larger dimension and get percentage per = w / iw Else per = h / ih End If 'create new sizes based on percentages nw = iw * per nh = ih * per 'now save it Dim photo As New Bitmap(nw, nh, Imaging.PixelFormat.Format24bppRgb) photo.SetResolution(img.HorizontalResolution, img.VerticalResolution) Dim graphic As Graphics graphic.FromImage(photo) Dim img2 As Image graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) graphic.Dispose() photo.Save(p) lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih & "<br>New " & nw & "x" & nhElse lblTemp.Text = "No resize necessary." End If End Function -- Show quoteDavid Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... > Hey David, > > So this seems a pure GDI+ image processing issue. Does this problem occurs > only when the uploaded image is of certain format or is a common issue > that > will occur for any images that uploaded onto the server? Generally I think > we can throubleshoot through the following steps: > > 1. Make sure that the image is uploaded correctly onto the server , open > it > in image viewer to make sure the data is not corrupted. > > 2. Then, use some standard image resizeing code to resize the image... > here is some simple GDI+ code I picked from net which resize the image > through percentage value: > > static Image ScaleByPercent(Image imgPhoto, int Percent) > { > float nPercent = ((float)Percent/100); > > int sourceWidth = imgPhoto.Width; > int sourceHeight = imgPhoto.Height; > int sourceX = 0; > int sourceY = 0; > > int destX = 0; > int destY = 0; > int destWidth = (int)(sourceWidth * nPercent); > int destHeight = (int)(sourceHeight * nPercent); > > Bitmap bmPhoto = new Bitmap(destWidth, destHeight, > PixelFormat.Format24bppRgb); > bmPhoto.SetResolution(imgPhoto.HorizontalResolution, > imgPhoto.VerticalResolution); > > Graphics grPhoto = Graphics.FromImage(bmPhoto); > grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; > > grPhoto.DrawImage(imgPhoto, > new Rectangle(destX,destY,destWidth,destHeight), > new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), > GraphicsUnit.Pixel); > > grPhoto.Dispose(); > return bmPhoto; > } > > You can try resizing through the code also to see whether it can work > correctly.... > > If there're any other finding, please feel free to post here. > > Thanks, > > Steven Cheng > Microsoft Online Support > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > > > -------------------- > | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > | Subject: Re: Upload image and then resize it - A generic error occurred > in GDI+. > | Date: Fri, 9 Dec 2005 11:15:17 -0500 > | Lines: 270 > | MIME-Version: 1.0 > | Content-Type: multipart/alternative; > | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" > | X-Priority: 3 > | X-MSMail-Priority: Normal > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > | Newsgroups: microsoft.public.dotnet.framework.aspnet > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl > | Xref: TK2MSFTNGXA02.phx.gbl > microsoft.public.dotnet.framework.aspnet:363864 > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | > | Besides the point, I just realized my math is wrong, please don't > critique. I just need to resolve the resize issue. > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... > | Howdy, > | I have a function that uploads an image and that works great. I love > Nets built in upload, so much easier than 3rd party uploaders! > | Now I am making a public function that will take the path of the > uploaded image, and resize it with the provided dimensions. My function is > below. The current function is returning an error when run from the upload > function: A generic error occurred in GDI+. Not sure what exactly that > means. From what I can tell, no one really knows what it means.... Here's > my public function > | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal > h > As Integer) As String > | Dim img As System.Drawing.Image > | img = System.Drawing.Image.FromFile(p) > | Dim iw As Integer = img.Width > | Dim ih As Integer = img.Height > | Dim nw, nh As Integer > | Dim per As Decimal > | If iw > w Or ih > h Then 'check to see if resize is necessary > | If w > h Then 'get the larger dimension and get percentage > | per = iw / w > | Else > | per = ih / h > | End If > | 'create new sizes based on percentages > | nw = iw * per > | nh = ih * per > | 'now save it > | Dim size As New Size(nw, nh) > | Dim nimg As New Bitmap(img, size) > | nimg.Save(p) > | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & > nw > & "x" & nh > | Else > | lblTemp.Text = "No resize necessary." > | End If > | End Function > | and here is my code for the upload, abbreviated > | If picType = "image/jpeg" Or picType = "image/gif" Or > picType = "image/pjpeg" Or picType = "image/bmp" Then > | File.PostedFile.SaveAs(path) > | ResizeImage(path, 120, 95) > | lblError.Text = "The speaker photo uploaded > sucessfully! Make sure to click Update below to save changes." > | Else > | lblError.Text = "Invalid image format. Only JPG, > JPEG, GIF and BMP images are allowed." > | End If > | Again, the upload works great > | Thanks!! > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | > | > The object reference error is occuring on the DrawImage line, if that helps.
-- Show quoteDavid Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... > After reviewing your code, this function returns the resized image as an > image to the calling script. I need the resized image to be saved to disk. > My script is below. I get an error: Object reference not set to an > instance of an object, which pertains to the graphic object because when I > remove that chunk of code, I get the same GDI+ error. > > Thanks!! > > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As > Integer) As String > Dim img As System.Drawing.Image > img = System.Drawing.Image.FromFile(p) > Dim iw As Integer = img.Width > Dim ih As Integer = img.Height > Dim nw, nh As Integer > Dim per As Decimal > > If iw > w Or ih > h Then 'check to see if resize is necessary > If iw > ih Then 'get the larger dimension and get percentage > per = w / iw > Else > per = h / ih > End If > > 'create new sizes based on percentages > nw = iw * per > nh = ih * per > > 'now save it > Dim photo As New Bitmap(nw, nh, > Imaging.PixelFormat.Format24bppRgb) > photo.SetResolution(img.HorizontalResolution, > img.VerticalResolution) > Dim graphic As Graphics > graphic.FromImage(photo) > Dim img2 As Image > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) > > graphic.Dispose() > photo.Save(p) > > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih & > "<br>New " & nw & "x" & nh > Else > lblTemp.Text = "No resize necessary." > End If > End Function > > -- > David Lozzi > Web Applications Developer > dlozzi@(remove-this)delphi-ts.com > > > > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... >> Hey David, >> >> So this seems a pure GDI+ image processing issue. Does this problem >> occurs >> only when the uploaded image is of certain format or is a common issue >> that >> will occur for any images that uploaded onto the server? Generally I >> think >> we can throubleshoot through the following steps: >> >> 1. Make sure that the image is uploaded correctly onto the server , open >> it >> in image viewer to make sure the data is not corrupted. >> >> 2. Then, use some standard image resizeing code to resize the image... >> here is some simple GDI+ code I picked from net which resize the image >> through percentage value: >> >> static Image ScaleByPercent(Image imgPhoto, int Percent) >> { >> float nPercent = ((float)Percent/100); >> >> int sourceWidth = imgPhoto.Width; >> int sourceHeight = imgPhoto.Height; >> int sourceX = 0; >> int sourceY = 0; >> >> int destX = 0; >> int destY = 0; >> int destWidth = (int)(sourceWidth * nPercent); >> int destHeight = (int)(sourceHeight * nPercent); >> >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight, >> PixelFormat.Format24bppRgb); >> bmPhoto.SetResolution(imgPhoto.HorizontalResolution, >> imgPhoto.VerticalResolution); >> >> Graphics grPhoto = Graphics.FromImage(bmPhoto); >> grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; >> >> grPhoto.DrawImage(imgPhoto, >> new Rectangle(destX,destY,destWidth,destHeight), >> new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), >> GraphicsUnit.Pixel); >> >> grPhoto.Dispose(); >> return bmPhoto; >> } >> >> You can try resizing through the code also to see whether it can work >> correctly.... >> >> If there're any other finding, please feel free to post here. >> >> Thanks, >> >> Steven Cheng >> Microsoft Online Support >> >> Get Secure! www.microsoft.com/security >> (This posting is provided "AS IS", with no warranties, and confers no >> rights.) >> >> >> >> -------------------- >> | From: "David Lozzi" <DavidLozzi@nospam.nospam> >> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> >> | Subject: Re: Upload image and then resize it - A generic error occurred >> in GDI+. >> | Date: Fri, 9 Dec 2005 11:15:17 -0500 >> | Lines: 270 >> | MIME-Version: 1.0 >> | Content-Type: multipart/alternative; >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" >> | X-Priority: 3 >> | X-MSMail-Priority: Normal >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 >> | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> >> | Newsgroups: microsoft.public.dotnet.framework.aspnet >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl >> | Xref: TK2MSFTNGXA02.phx.gbl >> microsoft.public.dotnet.framework.aspnet:363864 >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet >> | >> | Besides the point, I just realized my math is wrong, please don't >> critique. I just need to resolve the resize issue. >> | -- >> | David Lozzi >> | Web Applications Developer >> | dlozzi@(remove-this)delphi-ts.com >> | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message >> news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... >> | Howdy, >> | I have a function that uploads an image and that works great. I love >> Nets built in upload, so much easier than 3rd party uploaders! >> | Now I am making a public function that will take the path of the >> uploaded image, and resize it with the provided dimensions. My function >> is >> below. The current function is returning an error when run from the >> upload >> function: A generic error occurred in GDI+. Not sure what exactly that >> means. From what I can tell, no one really knows what it means.... Here's >> my public function >> | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal >> h >> As Integer) As String >> | Dim img As System.Drawing.Image >> | img = System.Drawing.Image.FromFile(p) >> | Dim iw As Integer = img.Width >> | Dim ih As Integer = img.Height >> | Dim nw, nh As Integer >> | Dim per As Decimal >> | If iw > w Or ih > h Then 'check to see if resize is necessary >> | If w > h Then 'get the larger dimension and get >> percentage >> | per = iw / w >> | Else >> | per = ih / h >> | End If >> | 'create new sizes based on percentages >> | nw = iw * per >> | nh = ih * per >> | 'now save it >> | Dim size As New Size(nw, nh) >> | Dim nimg As New Bitmap(img, size) >> | nimg.Save(p) >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & >> nw >> & "x" & nh >> | Else >> | lblTemp.Text = "No resize necessary." >> | End If >> | End Function >> | and here is my code for the upload, abbreviated >> | If picType = "image/jpeg" Or picType = "image/gif" Or >> picType = "image/pjpeg" Or picType = "image/bmp" Then >> | File.PostedFile.SaveAs(path) >> | ResizeImage(path, 120, 95) >> | lblError.Text = "The speaker photo uploaded >> sucessfully! Make sure to click Update below to save changes." >> | Else >> | lblError.Text = "Invalid image format. Only JPG, >> JPEG, GIF and BMP images are allowed." >> | End If >> | Again, the upload works great >> | Thanks!! >> | -- >> | David Lozzi >> | Web Applications Developer >> | dlozzi@(remove-this)delphi-ts.com >> | >> | >> > > Thanks for your response David,
I think the NullReference exception is due to the graphic object is not correctly assigned. From the code you pasted: ================= Dim graphic As Graphics graphic.FromImage(photo) Dim img2 As Image graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) graphic.Dispose() photo.Save(p) ================= You use graphic.FromImage(photo), this will return an Graphics object instance, and you need to assign it to the Graphics reference, like: ......... Dim graphic As Graphics graphic = Graphics.FromImage(photo) ............. Thanks, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) -------------------- | From: "David Lozzi" <DavidLozzi@nospam.nospam> <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> | Subject: Re: Upload image and then resize it - A generic error occurred microsoft.public.dotnet.framework.aspnet:364219in GDI+. | Date: Mon, 12 Dec 2005 09:40:02 -0500 | Lines: 229 | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | X-RFC2646: Format=Flowed; Response | Message-ID: <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> | Newsgroups: microsoft.public.dotnet.framework.aspnet | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl Show quote | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | | The object reference error is occuring on the DrawImage line, if that helps. | | -- | David Lozzi | Web Applications Developer | dlozzi@(remove-this)delphi-ts.com | | | | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message | news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... | > After reviewing your code, this function returns the resized image as an | > image to the calling script. I need the resized image to be saved to disk. | > My script is below. I get an error: Object reference not set to an | > instance of an object, which pertains to the graphic object because when I | > remove that chunk of code, I get the same GDI+ error. | > | > Thanks!! | > | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As | > Integer) As String | > Dim img As System.Drawing.Image | > img = System.Drawing.Image.FromFile(p) | > Dim iw As Integer = img.Width | > Dim ih As Integer = img.Height | > Dim nw, nh As Integer | > Dim per As Decimal | > | > If iw > w Or ih > h Then 'check to see if resize is necessary | > If iw > ih Then 'get the larger dimension and get percentage | > per = w / iw | > Else | > per = h / ih | > End If | > | > 'create new sizes based on percentages | > nw = iw * per | > nh = ih * per | > | > 'now save it | > Dim photo As New Bitmap(nw, nh, | > Imaging.PixelFormat.Format24bppRgb) | > photo.SetResolution(img.HorizontalResolution, | > img.VerticalResolution) | > Dim graphic As Graphics | > graphic.FromImage(photo) | > Dim img2 As Image | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) | > | > graphic.Dispose() | > photo.Save(p) | > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih & | > "<br>New " & nw & "x" & nh | > Else | > lblTemp.Text = "No resize necessary." | > End If | > End Function | > | > -- | > David Lozzi | > Web Applications Developer | > dlozzi@(remove-this)delphi-ts.com | > | > | > | > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message | > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... | >> Hey David, | >> | >> So this seems a pure GDI+ image processing issue. Does this problem | >> occurs | >> only when the uploaded image is of certain format or is a common issue | >> that | >> will occur for any images that uploaded onto the server? Generally I | >> think | >> we can throubleshoot through the following steps: | >> | >> 1. Make sure that the image is uploaded correctly onto the server , open | >> it | >> in image viewer to make sure the data is not corrupted. | >> | >> 2. Then, use some standard image resizeing code to resize the image... | >> here is some simple GDI+ code I picked from net which resize the image | >> through percentage value: | >> | >> static Image ScaleByPercent(Image imgPhoto, int Percent) | >> { | >> float nPercent = ((float)Percent/100); | >> | >> int sourceWidth = imgPhoto.Width; | >> int sourceHeight = imgPhoto.Height; | >> int sourceX = 0; | >> int sourceY = 0; | >> | >> int destX = 0; | >> int destY = 0; | >> int destWidth = (int)(sourceWidth * nPercent); | >> int destHeight = (int)(sourceHeight * nPercent); | >> | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight, | >> PixelFormat.Format24bppRgb); | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolution, | >> imgPhoto.VerticalResolution); | >> | >> Graphics grPhoto = Graphics.FromImage(bmPhoto); | >> grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | >> | >> grPhoto.DrawImage(imgPhoto, | >> new Rectangle(destX,destY,destWidth,destHeight), | >> new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), | >> GraphicsUnit.Pixel); | >> | >> grPhoto.Dispose(); | >> return bmPhoto; | >> } | >> | >> You can try resizing through the code also to see whether it can work | >> correctly.... | >> | >> If there're any other finding, please feel free to post here. | >> | >> Thanks, | >> | >> Steven Cheng | >> Microsoft Online Support | >> | >> Get Secure! www.microsoft.com/security | >> (This posting is provided "AS IS", with no warranties, and confers no | >> rights.) | >> | >> | >> | >> -------------------- | >> | From: "David Lozzi" <DavidLozzi@nospam.nospam> | >> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> | >> | Subject: Re: Upload image and then resize it - A generic error occurred | >> in GDI+. | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500 | >> | Lines: 270 | >> | MIME-Version: 1.0 | >> | Content-Type: multipart/alternative; | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" | >> | X-Priority: 3 | >> | X-MSMail-Priority: Normal | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | >> | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl | >> | Xref: TK2MSFTNGXA02.phx.gbl | >> microsoft.public.dotnet.framework.aspnet:363864 | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | >> | | >> | Besides the point, I just realized my math is wrong, please don't | >> critique. I just need to resolve the resize issue. | >> | -- | >> | David Lozzi | >> | Web Applications Developer | >> | dlozzi@(remove-this)delphi-ts.com | >> | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message | >> news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... | >> | Howdy, | >> | I have a function that uploads an image and that works great. I love | >> Nets built in upload, so much easier than 3rd party uploaders! | >> | Now I am making a public function that will take the path of the | >> uploaded image, and resize it with the provided dimensions. My function | >> is | >> below. The current function is returning an error when run from the | >> upload | >> function: A generic error occurred in GDI+. Not sure what exactly that | >> means. From what I can tell, no one really knows what it means.... Here's | >> my public function | >> | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal | >> h | >> As Integer) As String | >> | Dim img As System.Drawing.Image | >> | img = System.Drawing.Image.FromFile(p) | >> | Dim iw As Integer = img.Width | >> | Dim ih As Integer = img.Height | >> | Dim nw, nh As Integer | >> | Dim per As Decimal | >> | If iw > w Or ih > h Then 'check to see if resize is necessary | >> | If w > h Then 'get the larger dimension and get | >> percentage | >> | per = iw / w | >> | Else | >> | per = ih / h | >> | End If | >> | 'create new sizes based on percentages | >> | nw = iw * per | >> | nh = ih * per | >> | 'now save it | >> | Dim size As New Size(nw, nh) | >> | Dim nimg As New Bitmap(img, size) | >> | nimg.Save(p) | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & | >> nw | >> & "x" & nh | >> | Else | >> | lblTemp.Text = "No resize necessary." | >> | End If | >> | End Function | >> | and here is my code for the upload, abbreviated | >> | If picType = "image/jpeg" Or picType = "image/gif" Or | >> picType = "image/pjpeg" Or picType = "image/bmp" Then | >> | File.PostedFile.SaveAs(path) | >> | ResizeImage(path, 120, 95) | >> | lblError.Text = "The speaker photo uploaded | >> sucessfully! Make sure to click Update below to save changes." | >> | Else | >> | lblError.Text = "Invalid image format. Only JPG, | >> JPEG, GIF and BMP images are allowed." | >> | End If | >> | Again, the upload works great | >> | Thanks!! | >> | -- | >> | David Lozzi | >> | Web Applications Developer | >> | dlozzi@(remove-this)delphi-ts.com | >> | | >> | | >> | > | > | | | Hi David,
Any further progress on this issue? if there're anything else we can help, please feel free to post here. Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) -------------------- | X-Tomcat-ID: 53188289 <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> | MIME-Version: 1.0 microsoft.public.dotnet.framework.aspnet:364416| Content-Type: text/plain | Content-Transfer-Encoding: 7bit | From: stch***@online.microsoft.com (Steven Cheng[MSFT]) | Organization: Microsoft | Date: Tue, 13 Dec 2005 02:45:03 GMT | Subject: Re: Upload image and then resize it - A generic error occurred in GDI+. | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | Message-ID: <Mhr2B94$FHA.3***@TK2MSFTNGXA02.phx.gbl> | Newsgroups: microsoft.public.dotnet.framework.aspnet | Lines: 268 | Path: TK2MSFTNGXA02.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl Show quote | NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182 TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl| | Thanks for your response David, | | I think the NullReference exception is due to the graphic object is not | correctly assigned. From the code you pasted: | | ================= | Dim graphic As Graphics | graphic.FromImage(photo) | Dim img2 As Image | graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New | Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) | | graphic.Dispose() | photo.Save(p) | ================= | | You use graphic.FromImage(photo), this will return an Graphics object | instance, and you need to assign it to the Graphics reference, like: | | | ........ | Dim graphic As Graphics | graphic = Graphics.FromImage(photo) | ............ | | | Thanks, | | Steven Cheng | Microsoft Online Support | | Get Secure! www.microsoft.com/security | (This posting is provided "AS IS", with no warranties, and confers no | rights.) | | | | | -------------------- | | From: "David Lozzi" <DavidLozzi@nospam.nospam> | | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> | <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> | <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> | | Subject: Re: Upload image and then resize it - A generic error occurred | in GDI+. | | Date: Mon, 12 Dec 2005 09:40:02 -0500 | | Lines: 229 | | X-Priority: 3 | | X-MSMail-Priority: Normal | | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | | X-RFC2646: Format=Flowed; Response | | Message-ID: <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> | | Newsgroups: microsoft.public.dotnet.framework.aspnet | | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl | | Xref: TK2MSFTNGXA02.phx.gbl | microsoft.public.dotnet.framework.aspnet:364219 | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | | | | The object reference error is occuring on the DrawImage line, if that | helps. | | | | -- | | David Lozzi | | Web Applications Developer | | dlozzi@(remove-this)delphi-ts.com | | | | | | | | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message | | news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... | | > After reviewing your code, this function returns the resized image as | an | | > image to the calling script. I need the resized image to be saved to | disk. | | > My script is below. I get an error: Object reference not set to an | | > instance of an object, which pertains to the graphic object because | when I | | > remove that chunk of code, I get the same GDI+ error. | | > | | > Thanks!! | | > | | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h | As | | > Integer) As String | | > Dim img As System.Drawing.Image | | > img = System.Drawing.Image.FromFile(p) | | > Dim iw As Integer = img.Width | | > Dim ih As Integer = img.Height | | > Dim nw, nh As Integer | | > Dim per As Decimal | | > | | > If iw > w Or ih > h Then 'check to see if resize is necessary | | > If iw > ih Then 'get the larger dimension and get percentage | | > per = w / iw | | > Else | | > per = h / ih | | > End If | | > | | > 'create new sizes based on percentages | | > nw = iw * per | | > nh = ih * per | | > | | > 'now save it | | > Dim photo As New Bitmap(nw, nh, | | > Imaging.PixelFormat.Format24bppRgb) | | > photo.SetResolution(img.HorizontalResolution, | | > img.VerticalResolution) | | > Dim graphic As Graphics | | > graphic.FromImage(photo) | | > Dim img2 As Image | | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New | | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) | | > | | > graphic.Dispose() | | > photo.Save(p) | | > | | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih | & | | > "<br>New " & nw & "x" & nh | | > Else | | > lblTemp.Text = "No resize necessary." | | > End If | | > End Function | | > | | > -- | | > David Lozzi | | > Web Applications Developer | | > dlozzi@(remove-this)delphi-ts.com | | > | | > | | > | | > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message | | > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... | | >> Hey David, | | >> | | >> So this seems a pure GDI+ image processing issue. Does this problem | | >> occurs | | >> only when the uploaded image is of certain format or is a common issue | | >> that | | >> will occur for any images that uploaded onto the server? Generally I | | >> think | | >> we can throubleshoot through the following steps: | | >> | | >> 1. Make sure that the image is uploaded correctly onto the server , | open | | >> it | | >> in image viewer to make sure the data is not corrupted. | | >> | | >> 2. Then, use some standard image resizeing code to resize the image... | | >> here is some simple GDI+ code I picked from net which resize the image | | >> through percentage value: | | >> | | >> static Image ScaleByPercent(Image imgPhoto, int Percent) | | >> { | | >> float nPercent = ((float)Percent/100); | | >> | | >> int sourceWidth = imgPhoto.Width; | | >> int sourceHeight = imgPhoto.Height; | | >> int sourceX = 0; | | >> int sourceY = 0; | | >> | | >> int destX = 0; | | >> int destY = 0; | | >> int destWidth = (int)(sourceWidth * nPercent); | | >> int destHeight = (int)(sourceHeight * nPercent); | | >> | | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight, | | >> PixelFormat.Format24bppRgb); | | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolution, | | >> imgPhoto.VerticalResolution); | | >> | | >> Graphics grPhoto = Graphics.FromImage(bmPhoto); | | >> grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | | >> | | >> grPhoto.DrawImage(imgPhoto, | | >> new Rectangle(destX,destY,destWidth,destHeight), | | >> new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), | | >> GraphicsUnit.Pixel); | | >> | | >> grPhoto.Dispose(); | | >> return bmPhoto; | | >> } | | >> | | >> You can try resizing through the code also to see whether it can work | | >> correctly.... | | >> | | >> If there're any other finding, please feel free to post here. | | >> | | >> Thanks, | | >> | | >> Steven Cheng | | >> Microsoft Online Support | | >> | | >> Get Secure! www.microsoft.com/security | | >> (This posting is provided "AS IS", with no warranties, and confers no | | >> rights.) | | >> | | >> | | >> | | >> -------------------- | | >> | From: "David Lozzi" <DavidLozzi@nospam.nospam> | | >> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> | | >> | Subject: Re: Upload image and then resize it - A generic error | occurred | | >> in GDI+. | | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500 | | >> | Lines: 270 | | >> | MIME-Version: 1.0 | | >> | Content-Type: multipart/alternative; | | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" | | >> | X-Priority: 3 | | >> | X-MSMail-Priority: Normal | | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | | >> | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet | | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | | >> | Path: Show quote | | >> | Xref: TK2MSFTNGXA02.phx.gbl | | >> microsoft.public.dotnet.framework.aspnet:363864 | | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | | >> | | | >> | Besides the point, I just realized my math is wrong, please don't | | >> critique. I just need to resolve the resize issue. | | >> | -- | | >> | David Lozzi | | >> | Web Applications Developer | | >> | dlozzi@(remove-this)delphi-ts.com | | >> | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message | | >> news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... | | >> | Howdy, | | >> | I have a function that uploads an image and that works great. I | love | | >> Nets built in upload, so much easier than 3rd party uploaders! | | >> | Now I am making a public function that will take the path of the | | >> uploaded image, and resize it with the provided dimensions. My | function | | >> is | | >> below. The current function is returning an error when run from the | | >> upload | | >> function: A generic error occurred in GDI+. Not sure what exactly that | | >> means. From what I can tell, no one really knows what it means.... | Here's | | >> my public function | | >> | Function ResizeImage(ByVal p As String, ByVal w As Integer, | ByVal | | >> h | | >> As Integer) As String | | >> | Dim img As System.Drawing.Image | | >> | img = System.Drawing.Image.FromFile(p) | | >> | Dim iw As Integer = img.Width | | >> | Dim ih As Integer = img.Height | | >> | Dim nw, nh As Integer | | >> | Dim per As Decimal | | >> | If iw > w Or ih > h Then 'check to see if resize is | necessary | | >> | If w > h Then 'get the larger dimension and get | | >> percentage | | >> | per = iw / w | | >> | Else | | >> | per = ih / h | | >> | End If | | >> | 'create new sizes based on percentages | | >> | nw = iw * per | | >> | nh = ih * per | | >> | 'now save it | | >> | Dim size As New Size(nw, nh) | | >> | Dim nimg As New Bitmap(img, size) | | >> | nimg.Save(p) | | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " | & | | >> nw | | >> & "x" & nh | | >> | Else | | >> | lblTemp.Text = "No resize necessary." | | >> | End If | | >> | End Function | | >> | and here is my code for the upload, abbreviated | | >> | If picType = "image/jpeg" Or picType = "image/gif" | Or | | >> picType = "image/pjpeg" Or picType = "image/bmp" Then | | >> | File.PostedFile.SaveAs(path) | | >> | ResizeImage(path, 120, 95) | | >> | lblError.Text = "The speaker photo uploaded | | >> sucessfully! Make sure to click Update below to save changes." | | >> | Else | | >> | lblError.Text = "Invalid image format. Only | JPG, | | >> JPEG, GIF and BMP images are allowed." | | >> | End If | | >> | Again, the upload works great | | >> | Thanks!! | | >> | -- | | >> | David Lozzi | | >> | Web Applications Developer | | >> | dlozzi@(remove-this)delphi-ts.com | | >> | | | >> | | | >> | | > | | > | | | | | | | | I updated the script a bit and also included your last post. Here's my current script which is still receiving the GDI+ genereic error. I removed the Try statement this code was in and it appears to be erroring on the line with the *
Dim img As Image img = Image.FromFile(p) Dim photo As New Bitmap(img) Dim photo2 As New Bitmap(img.HorizontalResolution, img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb) Dim grPhoto As Graphics grPhoto = Graphics.FromImage(photo) grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih)) grPhoto.Dispose() photo.Dispose() photo2.SetResolution(nw, nh) photo2.Save(p) * photo2.Dispose() Thanks!! -- Show quoteDavid Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:Mhr2B94$FHA.3764@TK2MSFTNGXA02.phx.gbl... > Thanks for your response David, > > I think the NullReference exception is due to the graphic object is not > correctly assigned. From the code you pasted: > > ================= > Dim graphic As Graphics > graphic.FromImage(photo) > Dim img2 As Image > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) > > graphic.Dispose() > photo.Save(p) > ================= > > You use graphic.FromImage(photo), this will return an Graphics object > instance, and you need to assign it to the Graphics reference, like: > > > ........ > Dim graphic As Graphics > graphic = Graphics.FromImage(photo) > ............ > > > Thanks, > > Steven Cheng > Microsoft Online Support > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > > > > -------------------- > | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> > <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> > | Subject: Re: Upload image and then resize it - A generic error occurred > in GDI+. > | Date: Mon, 12 Dec 2005 09:40:02 -0500 > | Lines: 229 > | X-Priority: 3 > | X-MSMail-Priority: Normal > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | X-RFC2646: Format=Flowed; Response > | Message-ID: <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> > | Newsgroups: microsoft.public.dotnet.framework.aspnet > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl > | Xref: TK2MSFTNGXA02.phx.gbl > microsoft.public.dotnet.framework.aspnet:364219 > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | > | The object reference error is occuring on the DrawImage line, if that > helps. > | > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | > | > | > | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > | news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... > | > After reviewing your code, this function returns the resized image as > an > | > image to the calling script. I need the resized image to be saved to > disk. > | > My script is below. I get an error: Object reference not set to an > | > instance of an object, which pertains to the graphic object because > when I > | > remove that chunk of code, I get the same GDI+ error. > | > > | > Thanks!! > | > > | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h > As > | > Integer) As String > | > Dim img As System.Drawing.Image > | > img = System.Drawing.Image.FromFile(p) > | > Dim iw As Integer = img.Width > | > Dim ih As Integer = img.Height > | > Dim nw, nh As Integer > | > Dim per As Decimal > | > > | > If iw > w Or ih > h Then 'check to see if resize is necessary > | > If iw > ih Then 'get the larger dimension and get percentage > | > per = w / iw > | > Else > | > per = h / ih > | > End If > | > > | > 'create new sizes based on percentages > | > nw = iw * per > | > nh = ih * per > | > > | > 'now save it > | > Dim photo As New Bitmap(nw, nh, > | > Imaging.PixelFormat.Format24bppRgb) > | > photo.SetResolution(img.HorizontalResolution, > | > img.VerticalResolution) > | > Dim graphic As Graphics > | > graphic.FromImage(photo) > | > Dim img2 As Image > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) > | > > | > graphic.Dispose() > | > photo.Save(p) > | > > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih > & > | > "<br>New " & nw & "x" & nh > | > Else > | > lblTemp.Text = "No resize necessary." > | > End If > | > End Function > | > > | > -- > | > David Lozzi > | > Web Applications Developer > | > dlozzi@(remove-this)delphi-ts.com > | > > | > > | > > | > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message > | > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... > | >> Hey David, > | >> > | >> So this seems a pure GDI+ image processing issue. Does this problem > | >> occurs > | >> only when the uploaded image is of certain format or is a common issue > | >> that > | >> will occur for any images that uploaded onto the server? Generally I > | >> think > | >> we can throubleshoot through the following steps: > | >> > | >> 1. Make sure that the image is uploaded correctly onto the server , > open > | >> it > | >> in image viewer to make sure the data is not corrupted. > | >> > | >> 2. Then, use some standard image resizeing code to resize the image... > | >> here is some simple GDI+ code I picked from net which resize the image > | >> through percentage value: > | >> > | >> static Image ScaleByPercent(Image imgPhoto, int Percent) > | >> { > | >> float nPercent = ((float)Percent/100); > | >> > | >> int sourceWidth = imgPhoto.Width; > | >> int sourceHeight = imgPhoto.Height; > | >> int sourceX = 0; > | >> int sourceY = 0; > | >> > | >> int destX = 0; > | >> int destY = 0; > | >> int destWidth = (int)(sourceWidth * nPercent); > | >> int destHeight = (int)(sourceHeight * nPercent); > | >> > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight, > | >> PixelFormat.Format24bppRgb); > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolution, > | >> imgPhoto.VerticalResolution); > | >> > | >> Graphics grPhoto = Graphics.FromImage(bmPhoto); > | >> grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; > | >> > | >> grPhoto.DrawImage(imgPhoto, > | >> new Rectangle(destX,destY,destWidth,destHeight), > | >> new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), > | >> GraphicsUnit.Pixel); > | >> > | >> grPhoto.Dispose(); > | >> return bmPhoto; > | >> } > | >> > | >> You can try resizing through the code also to see whether it can work > | >> correctly.... > | >> > | >> If there're any other finding, please feel free to post here. > | >> > | >> Thanks, > | >> > | >> Steven Cheng > | >> Microsoft Online Support > | >> > | >> Get Secure! www.microsoft.com/security > | >> (This posting is provided "AS IS", with no warranties, and confers no > | >> rights.) > | >> > | >> > | >> > | >> -------------------- > | >> | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | >> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > | >> | Subject: Re: Upload image and then resize it - A generic error > occurred > | >> in GDI+. > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500 > | >> | Lines: 270 > | >> | MIME-Version: 1.0 > | >> | Content-Type: multipart/alternative; > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" > | >> | X-Priority: 3 > | >> | X-MSMail-Priority: Normal > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | >> | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl > | >> | Xref: TK2MSFTNGXA02.phx.gbl > | >> microsoft.public.dotnet.framework.aspnet:363864 > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | >> | > | >> | Besides the point, I just realized my math is wrong, please don't > | >> critique. I just need to resolve the resize issue. > | >> | -- > | >> | David Lozzi > | >> | Web Applications Developer > | >> | dlozzi@(remove-this)delphi-ts.com > | >> | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > | >> news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... > | >> | Howdy, > | >> | I have a function that uploads an image and that works great. I > love > | >> Nets built in upload, so much easier than 3rd party uploaders! > | >> | Now I am making a public function that will take the path of the >| >> uploaded image, and resize it with the provided dimensions. My > function > | >> is > | >> below. The current function is returning an error when run from the > | >> upload > | >> function: A generic error occurred in GDI+. Not sure what exactly that > | >> means. From what I can tell, no one really knows what it means.... > Here's > | >> my public function > | >> | Function ResizeImage(ByVal p As String, ByVal w As Integer, > ByVal > | >> h > | >> As Integer) As String > | >> | Dim img As System.Drawing.Image > | >> | img = System.Drawing.Image.FromFile(p) > | >> | Dim iw As Integer = img.Width > | >> | Dim ih As Integer = img.Height > | >> | Dim nw, nh As Integer > | >> | Dim per As Decimal > | >> | If iw > w Or ih > h Then 'check to see if resize is > necessary > | >> | If w > h Then 'get the larger dimension and get > | >> percentage > | >> | per = iw / w > | >> | Else > | >> | per = ih / h > | >> | End If > | >> | 'create new sizes based on percentages > | >> | nw = iw * per > | >> | nh = ih * per > | >> | 'now save it > | >> | Dim size As New Size(nw, nh) > | >> | Dim nimg As New Bitmap(img, size) > | >> | nimg.Save(p) > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " > & > | >> nw > | >> & "x" & nh > | >> | Else > | >> | lblTemp.Text = "No resize necessary." > | >> | End If > | >> | End Function > | >> | and here is my code for the upload, abbreviated > | >> | If picType = "image/jpeg" Or picType = "image/gif" > Or > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then > | >> | File.PostedFile.SaveAs(path) > | >> | ResizeImage(path, 120, 95) > | >> | lblError.Text = "The speaker photo uploaded > | >> sucessfully! Make sure to click Update below to save changes." > | >> | Else > | >> | lblError.Text = "Invalid image format. Only > JPG, > | >> JPEG, GIF and BMP images are allowed." > | >> | End If > | >> | Again, the upload works great > | >> | Thanks!! > | >> | -- > | >> | David Lozzi > | >> | Web Applications Developer > | >> | dlozzi@(remove-this)delphi-ts.com > | >> | > | >> | > | >> > | > > | > > | > | > | > If I change the path to a static path like c:\photos\image.gif it writes an image file, however the image is black, but appears to be to the correct dimensions.
Thanks, -- David Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message news:ufd9LfbAGHA.3968@TK2MSFTNGP10.phx.gbl... I updated the script a bit and also included your last post. Here's my current script which is still receiving the GDI+ genereic error. I removed the Try statement this code was in and it appears to be erroring on the line with the *Dim img As Image img = Image.FromFile(p) Dim photo As New Bitmap(img) Dim photo2 As New Bitmap(img.HorizontalResolution, img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb) Dim grPhoto As Graphics grPhoto = Graphics.FromImage(photo) grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih)) grPhoto.Dispose() photo.Dispose() photo2.SetResolution(nw, nh) photo2.Save(p) * photo2.Dispose() Thanks!! -- David Lozzi Web Applications Developer dlozzi@(remove-this)delphi-ts.com Show quote "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:Mhr2B94$FHA.3764@TK2MSFTNGXA02.phx.gbl... > Thanks for your response David, > > I think the NullReference exception is due to the graphic object is not > correctly assigned. From the code you pasted: > > ================= > Dim graphic As Graphics > graphic.FromImage(photo) > Dim img2 As Image > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) > > graphic.Dispose() > photo.Save(p) > ================= > > You use graphic.FromImage(photo), this will return an Graphics object > instance, and you need to assign it to the Graphics reference, like: > > > ........ > Dim graphic As Graphics > graphic = Graphics.FromImage(photo) > ............ > > > Thanks, > > Steven Cheng > Microsoft Online Support > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > > > > -------------------- > | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> > <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> > | Subject: Re: Upload image and then resize it - A generic error occurred > in GDI+. > | Date: Mon, 12 Dec 2005 09:40:02 -0500 > | Lines: 229 > | X-Priority: 3 > | X-MSMail-Priority: Normal > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | X-RFC2646: Format=Flowed; Response > | Message-ID: <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> > | Newsgroups: microsoft.public.dotnet.framework.aspnet > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl > | Xref: TK2MSFTNGXA02.phx.gbl > microsoft.public.dotnet.framework.aspnet:364219 > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | > | The object reference error is occuring on the DrawImage line, if that > helps. > | > | -- > | David Lozzi > | Web Applications Developer > | dlozzi@(remove-this)delphi-ts.com > | > | > | > | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > | news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... > | > After reviewing your code, this function returns the resized image as > an > | > image to the calling script. I need the resized image to be saved to > disk. > | > My script is below. I get an error: Object reference not set to an > | > instance of an object, which pertains to the graphic object because > when I > | > remove that chunk of code, I get the same GDI+ error. > | > > | > Thanks!! > | > > | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h > As > | > Integer) As String > | > Dim img As System.Drawing.Image > | > img = System.Drawing.Image.FromFile(p) > | > Dim iw As Integer = img.Width > | > Dim ih As Integer = img.Height > | > Dim nw, nh As Integer > | > Dim per As Decimal > | > > | > If iw > w Or ih > h Then 'check to see if resize is necessary > | > If iw > ih Then 'get the larger dimension and get percentage > | > per = w / iw > | > Else > | > per = h / ih > | > End If > | > > | > 'create new sizes based on percentages > | > nw = iw * per > | > nh = ih * per > | > > | > 'now save it > | > Dim photo As New Bitmap(nw, nh, > | > Imaging.PixelFormat.Format24bppRgb) > | > photo.SetResolution(img.HorizontalResolution, > | > img.VerticalResolution) > | > Dim graphic As Graphics > | > graphic.FromImage(photo) > | > Dim img2 As Image > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) > | > > | > graphic.Dispose() > | > photo.Save(p) > | > > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih > & > | > "<br>New " & nw & "x" & nh > | > Else > | > lblTemp.Text = "No resize necessary." > | > End If > | > End Function > | > > | > -- > | > David Lozzi > | > Web Applications Developer > | > dlozzi@(remove-this)delphi-ts.com > | > > | > > | > > | > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message > | > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... > | >> Hey David, > | >> > | >> So this seems a pure GDI+ image processing issue. Does this problem > | >> occurs > | >> only when the uploaded image is of certain format or is a common issue > | >> that > | >> will occur for any images that uploaded onto the server? Generally I > | >> think > | >> we can throubleshoot through the following steps: > | >> > | >> 1. Make sure that the image is uploaded correctly onto the server , > open > | >> it > | >> in image viewer to make sure the data is not corrupted. > | >> > | >> 2. Then, use some standard image resizeing code to resize the image... > | >> here is some simple GDI+ code I picked from net which resize the image > | >> through percentage value: > | >> > | >> static Image ScaleByPercent(Image imgPhoto, int Percent) > | >> { > | >> float nPercent = ((float)Percent/100); > | >> > | >> int sourceWidth = imgPhoto.Width; > | >> int sourceHeight = imgPhoto.Height; > | >> int sourceX = 0; > | >> int sourceY = 0; > | >> > | >> int destX = 0; > | >> int destY = 0; > | >> int destWidth = (int)(sourceWidth * nPercent); > | >> int destHeight = (int)(sourceHeight * nPercent); > | >> > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight, > | >> PixelFormat.Format24bppRgb); > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolution, > | >> imgPhoto.VerticalResolution); > | >> > | >> Graphics grPhoto = Graphics.FromImage(bmPhoto); > | >> grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; > | >> > | >> grPhoto.DrawImage(imgPhoto, > | >> new Rectangle(destX,destY,destWidth,destHeight), > | >> new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), > | >> GraphicsUnit.Pixel); > | >> > | >> grPhoto.Dispose(); > | >> return bmPhoto; > | >> } > | >> > | >> You can try resizing through the code also to see whether it can work > | >> correctly.... > | >> > | >> If there're any other finding, please feel free to post here. > | >> > | >> Thanks, > | >> > | >> Steven Cheng > | >> Microsoft Online Support > | >> > | >> Get Secure! www.microsoft.com/security > | >> (This posting is provided "AS IS", with no warranties, and confers no > | >> rights.) > | >> > | >> > | >> > | >> -------------------- > | >> | From: "David Lozzi" <DavidLozzi@nospam.nospam> > | >> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> > | >> | Subject: Re: Upload image and then resize it - A generic error > occurred > | >> in GDI+. > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500 > | >> | Lines: 270 > | >> | MIME-Version: 1.0 > | >> | Content-Type: multipart/alternative; > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0" > | >> | X-Priority: 3 > | >> | X-MSMail-Priority: Normal > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 > | >> | Message-ID: <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 > | >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl > | >> | Xref: TK2MSFTNGXA02.phx.gbl > | >> microsoft.public.dotnet.framework.aspnet:363864 > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet > | >> | > | >> | Besides the point, I just realized my math is wrong, please don't > | >> critique. I just need to resolve the resize issue. > | >> | -- > | >> | David Lozzi > | >> | Web Applications Developer > | >> | dlozzi@(remove-this)delphi-ts.com > | >> | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message > | >> news:%23r9oVoN$FHA.1312@TK2MSFTNGP09.phx.gbl... > | >> | Howdy, > | >> | I have a function that uploads an image and that works great. I > love > | >> Nets built in upload, so much easier than 3rd party uploaders! > | >> | Now I am making a public function that will take the path of the >| >> uploaded image, and resize it with the provided dimensions. My > function > | >> is > | >> below. The current function is returning an error when run from the > | >> upload > | >> function: A generic error occurred in GDI+. Not sure what exactly that > | >> means. From what I can tell, no one really knows what it means.... > Here's > | >> my public function > | >> | Function ResizeImage(ByVal p As String, ByVal w As Integer, > ByVal > | >> h > | >> As Integer) As String > | >> | Dim img As System.Drawing.Image > | >> | img = System.Drawing.Image.FromFile(p) > | >> | Dim iw As Integer = img.Width > | >> | Dim ih As Integer = img.Height > | >> | Dim nw, nh As Integer > | >> | Dim per As Decimal > | >> | If iw > w Or ih > h Then 'check to see if resize is > necessary > | >> | If w > h Then 'get the larger dimension and get > | >> percentage > | >> | per = iw / w > | >> | Else > | >> | per = ih / h > | >> | End If > | >> | 'create new sizes based on percentages > | >> | nw = iw * per > | >> | nh = ih * per > | >> | 'now save it > | >> | Dim size As New Size(nw, nh) > | >> | Dim nimg As New Bitmap(img, size) > | >> | nimg.Save(p) > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " > & > | >> nw > | >> & "x" & nh > | >> | Else > | >> | lblTemp.Text = "No resize necessary." > | >> | End If > | >> | End Function > | >> | and here is my code for the upload, abbreviated > | >> | If picType = "image/jpeg" Or picType = "image/gif" > Or > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then > | >> | File.PostedFile.SaveAs(path) > | >> | ResizeImage(path, 120, 95) > | >> | lblError.Text = "The speaker photo uploaded > | >> sucessfully! Make sure to click Update below to save changes." > | >> | Else > | >> | lblError.Text = "Invalid image format. Only > JPG, > | >> JPEG, GIF and BMP images are allowed." > | >> | End If > | >> | Again, the upload works great > | >> | Thanks!! > | >> | -- > | >> | David Lozzi > | >> | Web Applications Developer > | >> | dlozzi@(remove-this)delphi-ts.com > | >> | > | >> | > | >> > | > > | > > | > | > | > Hi David, Thanks for your followup. Seems that the code you used is not quite correct. The Graphics.DrawImage method is used as following: the graphics object itself should be created from the Target image (the resized one ....). And the Image Parameter passed into the DrawImage should be the source image..... Here is the function I pasted previously, I've converted it into VB.NET for your reference.... ============================== Private Sub btnSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSize.Click Dim img As Image = Image.FromFile("Picture1.jpg") Dim imgSize As Image = ScaleByPercent(img, 50) imgSize.Save("ResizedPicture1.jpg") End Sub Shared Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As Integer) As Image Dim nPercent As Single = (CType(Percent / 100, Single)) Dim sourceWidth As Integer = imgPhoto.Width Dim sourceHeight As Integer = imgPhoto.Height Dim sourceX As Integer = 0 Dim sourceY As Integer = 0 Dim destX As Integer = 0 Dim destY As Integer = 0 Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer) Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer) Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb) bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution) Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto) grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic grPhoto.DrawImage(imgPhoto, _ New Rectangle(destX, destY, destWidth, destHeight), _ New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), _ GraphicsUnit.Pixel) grPhoto.Dispose() Return bmPhoto End Function ============================== Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) -------------------- | From: "David Lozzi" <DavidLozzi@nospam.nospam> <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> <Mhr2B94$FHA.3***@TK2MSFTNGXA02.phx.gbl> <ufd9LfbAGHA.3***@TK2MSFTNGP10.phx.gbl> Show quote | Subject: Re: Upload image and then resize it - A generic error occurred microsoft.public.dotnet.framework.aspnet:365193in GDI+. | Date: Thu, 15 Dec 2005 15:49:22 -0500 | Lines: 1138 | MIME-Version: 1.0 | Content-Type: multipart/alternative; | boundary="----=_NextPart_000_0027_01C6018F.1E5A18D0" | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | Message-ID: <eiGwKkbAGHA.1***@TK2MSFTNGP11.phx.gbl> | Newsgroups: microsoft.public.dotnet.framework.aspnet | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl | Xref: TK2MSFTNGXA02.phx.gbl | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet an image file, however the image is black, but appears to be to the correct | | If I change the path to a static path like c:\photos\image.gif it writes dimensions. | Thanks, current script which is still receiving the GDI+ genereic error. I removed | -- | David Lozzi | Web Applications Developer | dlozzi@(remove-this)delphi-ts.com | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message news:ufd9LfbAGHA.3968@TK2MSFTNGP10.phx.gbl... | I updated the script a bit and also included your last post. Here's my the Try statement this code was in and it appears to be erroring on the line with the * | Dim img As Image img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)| img = Image.FromFile(p) | Dim photo As New Bitmap(img) | Dim photo2 As New Bitmap(img.HorizontalResolution, Show quote | Dim grPhoto As Graphics TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl| grPhoto = Graphics.FromImage(photo) | grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih)) | grPhoto.Dispose() | photo.Dispose() | photo2.SetResolution(nw, nh) | photo2.Save(p) * | photo2.Dispose() | Thanks!! | -- | David Lozzi | Web Applications Developer | dlozzi@(remove-this)delphi-ts.com | | "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:Mhr2B94$FHA.3764@TK2MSFTNGXA02.phx.gbl... | > Thanks for your response David, | > | > I think the NullReference exception is due to the graphic object is not | > correctly assigned. From the code you pasted: | > | > ================= | > Dim graphic As Graphics | > graphic.FromImage(photo) | > Dim img2 As Image | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) | > | > graphic.Dispose() | > photo.Save(p) | > ================= | > | > You use graphic.FromImage(photo), this will return an Graphics object | > instance, and you need to assign it to the Graphics reference, like: | > | > | > ........ | > Dim graphic As Graphics | > graphic = Graphics.FromImage(photo) | > ............ | > | > | > Thanks, | > | > Steven Cheng | > Microsoft Online Support | > | > Get Secure! www.microsoft.com/security | > (This posting is provided "AS IS", with no warranties, and confers no | > rights.) | > | > | > | > | > -------------------- | > | From: "David Lozzi" <DavidLozzi@nospam.nospam> | > | References: <#r9oVoN$FHA.1***@TK2MSFTNGP09.phx.gbl> | > <#GowAvN$FHA.2***@TK2MSFTNGP09.phx.gbl> | > <v1#WZyw$FHA.3***@TK2MSFTNGXA02.phx.gbl> | > <O382Lby$FHA.3***@TK2MSFTNGP15.phx.gbl> | > | Subject: Re: Upload image and then resize it - A generic error occurred | > in GDI+. | > | Date: Mon, 12 Dec 2005 09:40:02 -0500 | > | Lines: 229 | > | X-Priority: 3 | > | X-MSMail-Priority: Normal | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 | > | X-RFC2646: Format=Flowed; Response | > | Message-ID: <O$kR$ny$FHA.3***@TK2MSFTNGP15.phx.gbl> | > | Newsgroups: microsoft.public.dotnet.framework.aspnet | > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200 | > | Path: Show quote | > | Xref: TK2MSFTNGXA02.phx.gbl "x" & ih | > microsoft.public.dotnet.framework.aspnet:364219 | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet | > | | > | The object reference error is occuring on the DrawImage line, if that | > helps. | > | | > | -- | > | David Lozzi | > | Web Applications Developer | > | dlozzi@(remove-this)delphi-ts.com | > | | > | | > | | > | "David Lozzi" <DavidLozzi@nospam.nospam> wrote in message | > | news:O382Lby$FHA.3048@TK2MSFTNGP15.phx.gbl... | > | > After reviewing your code, this function returns the resized image as | > an | > | > image to the calling script. I need the resized image to be saved to | > disk. | > | > My script is below. I get an error: Object reference not set to an | > | > instance of an object, which pertains to the graphic object because | > when I | > | > remove that chunk of code, I get the same GDI+ error. | > | > | > | > Thanks!! | > | > | > | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h | > As | > | > Integer) As String | > | > Dim img As System.Drawing.Image | > | > img = System.Drawing.Image.FromFile(p) | > | > Dim iw As Integer = img.Width | > | > Dim ih As Integer = img.Height | > | > Dim nw, nh As Integer | > | > Dim per As Decimal | > | > | > | > If iw > w Or ih > h Then 'check to see if resize is necessary | > | > If iw > ih Then 'get the larger dimension and get percentage | > | > per = w / iw | > | > Else | > | > per = h / ih | > | > End If | > | > | > | > 'create new sizes based on percentages | > | > nw = iw * per | > | > nh = ih * per | > | > | > | > 'now save it | > | > Dim photo As New Bitmap(nw, nh, | > | > Imaging.PixelFormat.Format24bppRgb) | > | > photo.SetResolution(img.HorizontalResolution, | > | > img.VerticalResolution) | > | > Dim graphic As Graphics | > | > graphic.FromImage(photo) | > | > Dim img2 As Image | > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New | > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel) | > | > | > | > graphic.Dispose() | > | > photo.Save(p) | > | > | > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & Show quote | > & | > | > "<br>New " & nw & "x" & nh | > | > Else | > | > lblTemp.Text = "No resize necessary." | > | > End If | > | > End Function | > | > | > | > -- | > | > David Lozzi | > | > Web Applications Developer | > | > dlozzi@(remove-this)delphi-ts.com | > | > | > | > | > | > | > | > "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message | > | > news:v1%23WZyw$FHA.3764@TK2MSFTNGXA02.phx.gbl... | > | >> Hey David, | > | >> | > | >> So this seems a pure GDI+ image processing issue. Does this problem | > | >> occurs | > | >> only when the uploaded image is of certain format or is a common issue | > | >> that | > | >> will occur for any images that uploaded onto the server? Generally I | > | >> think | > | >> we can throubleshoot through the following steps: | > | >> | > | | |||||||||||||||||||||||