Upload image and then resize it - A generic error occurred in GDI+.

D

David Lozzi

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!!
 
D

David Lozzi

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



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!!
 
K

Kevin Yu [MSFT]

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."
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <#[email protected]>
| 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: <#[email protected]>
| 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
| 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
|
|
 
D

David Lozzi

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,

--
David Lozzi
Web Applications Developer
dlozzi@(remove-this)delphi-ts.com
 
D

David Lozzi

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
 
D

David Lozzi

The object reference error is occuring on the DrawImage line, if that helps.

--
David Lozzi
Web Applications Developer
dlozzi@(remove-this)delphi-ts.com
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
<v1#[email protected]>
<[email protected]>
| 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: <[email protected]>
| 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
|
|
|
| | > 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
| >
| >
| >
| > | >> 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" <[email protected]>
| >> | References: <#[email protected]>
| >> | 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: <#[email protected]>
| >> | 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
| >> | >> | 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
| >> |
| >> |
| >>
| >
| >
|
|
|
 
S

Steven Cheng[MSFT]

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
| References: <#[email protected]>
<#[email protected]>
<v1#[email protected]>
<[email protected]>
<[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 268
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364416
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
|
| 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" <[email protected]>
| | References: <#[email protected]>
| <#[email protected]>
| <v1#[email protected]>
| <[email protected]>
| | 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: <[email protected]>
| | 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
| |
| |
| |
| | | | > 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
| | >
| | >
| | >
| | > | | >> 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" <[email protected]>
| | >> | References: <#[email protected]>
| | >> | 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: <#[email protected]>
| | >> | 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
| | >> | | >> | 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
| | >> |
| | >> |
| | >>
| | >
| | >
| |
| |
| |
|
|
 
D

David Lozzi

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
 
D

David Lozzi

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



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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
<v1#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Upload image and then resize it - A generic error occurred
in 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: <[email protected]>
| 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
microsoft.public.dotnet.framework.aspnet:365193
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| 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
| 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
|
| > 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" <[email protected]>
| > | References: <#[email protected]>
| > <#[email protected]>
| > <v1#[email protected]>
| > <[email protected]>
| > | 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: <[email protected]>
| > | 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
| > |
| > |
| > |
| > | | > | > 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
| > | >
| > | >
| > | >
message
| > | > | > | >> 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" <[email protected]>
| > | >> | References: <#[email protected]>
| > | >> | 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: <#[email protected]>
| > | >> | 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
| > | >> | > | >> | 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
| > | >> |
| > | >> |
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|
 
D

David Lozzi

Allrighty then... It resizes and saves great if I specify a different folder
than the one I need to save it to. If i specify "c:\photos\img.gif" it saves
correctly, and resized! If I use the p variable I get the GDI+ error. My
script is below.

I commented out the line 'grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic()because VisualStudio complained about
the second InterpolationMode as "reference to a non-shared member requires
an object reference."

Thank you for you great support!!

File.PostedFile.SaveAs(path)

ResizeImage(path, 95, 120)


Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
Integer) As String

Dim img As Image = 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 bmPhoto As Bitmap = New Bitmap(nw, nh,
Imaging.PixelFormat.Format24bppRgb)

bmPhoto.SetResolution(img.HorizontalResolution,
img.VerticalResolution)



Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)

'grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic()



grPhoto.DrawImage(img, _

New Rectangle(0, 0, nw, nh), _

New Rectangle(0, 0, iw, ih), _

GraphicsUnit.Pixel)



grPhoto.Dispose()

Dim img2 As Image = bmPhoto



img2.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
 
S

Steven Cheng[MSFT]

Thanks for your followup David,

Well, now I got that you're saving the resized image object to the same
path as the original one..... Thus, it'll failed if we didn't release the
handle to the original image before we do the saving (which will try
overriding the original image file.....). Though IMO i'd suggest you
avoid overriding original image file, you can try the following code if you
do need to do so:

add the code to explicitly dispose the original image object and set
reference to nothing before we do the savning on the new resized image
object. So the modified image resiziing function should look like:

======================
Shared Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As
Integer) As Image

' same as before .............................

grPhoto.DrawImage(imgPhoto, _
New Rectangle(destX, destY, destWidth, destHeight), _
New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), _
GraphicsUnit.Pixel)

grPhoto.Dispose()

imgPhoto.Dispose()
imgPhoto = Nothing

Return bmPhoto
End Function
End Class
=======================

the difference is that we add the two statements:

imgPhoto.Dispose()
imgPhoto = Nothing

before returning the resized image object which explicitly dispose the
original image object(so as to release the handle....)

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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
<v1#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Fri, 16 Dec 2005 11:50:37 -0500
| Lines: 604
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#[email protected]>
| 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!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:365350
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Allrighty then... It resizes and saves great if I specify a different
folder
| than the one I need to save it to. If i specify "c:\photos\img.gif" it
saves
| correctly, and resized! If I use the p variable I get the GDI+ error. My
| script is below.
|
| I commented out the line 'grPhoto.InterpolationMode =
| InterpolationMode.HighQualityBicubic()because VisualStudio complained
about
| the second InterpolationMode as "reference to a non-shared member
requires
| an object reference."
|
| Thank you for you great support!!
|
| File.PostedFile.SaveAs(path)
|
| ResizeImage(path, 95, 120)
|
|
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
| Integer) As String
|
| Dim img As Image = 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 bmPhoto As Bitmap = New Bitmap(nw, nh,
| Imaging.PixelFormat.Format24bppRgb)
|
| bmPhoto.SetResolution(img.HorizontalResolution,
| img.VerticalResolution)
|
|
|
| Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
|
| 'grPhoto.InterpolationMode =
| InterpolationMode.HighQualityBicubic()
|
|
|
| grPhoto.DrawImage(img, _
|
| New Rectangle(0, 0, nw, nh), _
|
| New Rectangle(0, 0, iw, ih), _
|
| GraphicsUnit.Pixel)
|
|
|
| grPhoto.Dispose()
|
| Dim img2 As Image = bmPhoto
|
|
|
| img2.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
|
|
|
| | > 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" <[email protected]>
| > | References: <#[email protected]>
| > <#[email protected]>
| > <v1#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Upload image and then resize it - A generic error
occurred
| > in 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: <[email protected]>
| > | 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
| > microsoft.public.dotnet.framework.aspnet:365193
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | 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
| > | > | 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
| > |
| > | > | > 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" <[email protected]>
| > | > | References: <#[email protected]>
| > | > <#[email protected]>
| > | > <v1#[email protected]>
| > | > <[email protected]>
| > | > | 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: <[email protected]>
| > | > | 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
| > | > |
| > | > |
| > | > |
| > | > | | > | > | > 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
| > | > | >
| > | > | >
| > | > | >
| > message
| > | > | > | > | > | >> 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" <[email protected]>
| > | > | >> | References: <#[email protected]>
| > | > | >> | 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: <#[email protected]>
| > | > | >> | 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
| > | > | >> | > | > | >> | 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
| > | > | >> |
| > | > | >> |
| > | > | >>
| > | > | >
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| >
|
|
|
 
D

David Lozzi

GOT IT! I wasn't disposing of the img correctly. My final code is below for
all who are interested.

Thank you Steven for your great support with this!

Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
Integer) As String

Dim img As Image = 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 bmPhoto As Bitmap = New Bitmap(nw, nh,
Imaging.PixelFormat.Format24bppRgb)

bmPhoto.SetResolution(img.HorizontalResolution, img.VerticalResolution)



Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)



grPhoto.DrawImage(img, _

New Rectangle(0, 0, nw, nh), _

New Rectangle(0, 0, iw, ih), _

GraphicsUnit.Pixel)



grPhoto.Dispose()

grPhoto = Nothing

img.Dispose()

Dim img2 As Image = bmPhoto



img2.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
 
S

Steven Cheng[MSFT]

Cool!

Have a good day!

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" <[email protected]>
| References: <#[email protected]>
<#[email protected]>
<v1#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Mon, 19 Dec 2005 12:15:44 -0500
| Lines: 837
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#[email protected]>
| 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
microsoft.public.dotnet.framework.aspnet:365736
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| GOT IT! I wasn't disposing of the img correctly. My final code is below
for
| all who are interested.
|
| Thank you Steven for your great support with this!
|
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
| Integer) As String
|
| Dim img As Image = 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 bmPhoto As Bitmap = New Bitmap(nw, nh,
| Imaging.PixelFormat.Format24bppRgb)
|
| bmPhoto.SetResolution(img.HorizontalResolution, img.VerticalResolution)
|
|
|
| Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
|
|
|
| grPhoto.DrawImage(img, _
|
| New Rectangle(0, 0, nw, nh), _
|
| New Rectangle(0, 0, iw, ih), _
|
| GraphicsUnit.Pixel)
|
|
|
| grPhoto.Dispose()
|
| grPhoto = Nothing
|
| img.Dispose()
|
| Dim img2 As Image = bmPhoto
|
|
|
| img2.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
|
|
|
| | > Thanks for your followup David,
| >
| > Well, now I got that you're saving the resized image object to the same
| > path as the original one..... Thus, it'll failed if we didn't release
the
| > handle to the original image before we do the saving (which will try
| > overriding the original image file.....). Though IMO i'd suggest you
| > avoid overriding original image file, you can try the following code if
| > you
| > do need to do so:
| >
| > add the code to explicitly dispose the original image object and set
| > reference to nothing before we do the savning on the new resized image
| > object. So the modified image resiziing function should look like:
| >
| > ======================
| > Shared Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As
| > Integer) As Image
| >
| > ' same as before .............................
| >
| > grPhoto.DrawImage(imgPhoto, _
| > New Rectangle(destX, destY, destWidth, destHeight), _
| > New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), _
| > GraphicsUnit.Pixel)
| >
| > grPhoto.Dispose()
| >
| > imgPhoto.Dispose()
| > imgPhoto = Nothing
| >
| > Return bmPhoto
| > End Function
| > End Class
| > =======================
| >
| > the difference is that we add the two statements:
| >
| > imgPhoto.Dispose()
| > imgPhoto = Nothing
| >
| > before returning the resized image object which explicitly dispose the
| > original image object(so as to release the handle....)
| >
| > 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" <[email protected]>
| > | References: <#[email protected]>
| > <#[email protected]>
| > <v1#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Upload image and then resize it - A generic error
occurred
| > in GDI+.
| > | Date: Fri, 16 Dec 2005 11:50:37 -0500
| > | Lines: 604
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | Message-ID: <#[email protected]>
| > | 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!TK2MSFTNGP12.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:365350
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Allrighty then... It resizes and saves great if I specify a different
| > folder
| > | than the one I need to save it to. If i specify "c:\photos\img.gif" it
| > saves
| > | correctly, and resized! If I use the p variable I get the GDI+ error.
My
| > | script is below.
| > |
| > | I commented out the line 'grPhoto.InterpolationMode =
| > | InterpolationMode.HighQualityBicubic()because VisualStudio complained
| > about
| > | the second InterpolationMode as "reference to a non-shared member
| > requires
| > | an object reference."
| > |
| > | Thank you for you great support!!
| > |
| > | File.PostedFile.SaveAs(path)
| > |
| > | ResizeImage(path, 95, 120)
| > |
| > |
| > | Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
| > | Integer) As String
| > |
| > | Dim img As Image = 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 bmPhoto As Bitmap = New Bitmap(nw, nh,
| > | Imaging.PixelFormat.Format24bppRgb)
| > |
| > | bmPhoto.SetResolution(img.HorizontalResolution,
| > | img.VerticalResolution)
| > |
| > |
| > |
| > | Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
| > |
| > | 'grPhoto.InterpolationMode =
| > | InterpolationMode.HighQualityBicubic()
| > |
| > |
| > |
| > | grPhoto.DrawImage(img, _
| > |
| > | New Rectangle(0, 0, nw, nh), _
| > |
| > | New Rectangle(0, 0, iw, ih), _
| > |
| > | GraphicsUnit.Pixel)
| > |
| > |
| > |
| > | grPhoto.Dispose()
| > |
| > | Dim img2 As Image = bmPhoto
| > |
| > |
| > |
| > | img2.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
| > |
| > |
| > |
| > | | > | > 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" <[email protected]>
| > | > | References: <#[email protected]>
| > | > <#[email protected]>
| > | > <v1#[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > | Subject: Re: Upload image and then resize it - A generic error
| > occurred
| > | > in 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: <[email protected]>
| > | > | 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
| > | > microsoft.public.dotnet.framework.aspnet:365193
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > |
| > | > | 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
| > | > | > | > | 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
| > | > |
| > message
| > | > | > | > | > 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" <[email protected]>
| > | > | > | References: <#[email protected]>
| > | > | > <#[email protected]>
| > | > | > <v1#[email protected]>
| > | > | > <[email protected]>
| > | > | > | 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: <[email protected]>
| > | > | > | 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
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | > | | > | > | > | > 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
| > | > | > | >
| > | > | > | >
| > | > | > | >
in
| > | > message
| > | > | > | > | > | > | > | >> 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" <[email protected]>
| > | > | > | >> | References: <#[email protected]>
| > | > | > | >> | 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: <#[email protected]>
| > | > | > | >> | 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
| > message
| > | > | > | >> | > | > | > | >> | 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
| > | > | > | >> |
| > | > | > | >> |
| > | > | > | >>
| > | > | > | >
| > | > | > | >
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | >
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top