473,416 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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

Dec 9 '05 #1
15 5287
Besides the point, I just realized my math is wrong, please don't critique. I just need to resolve the resize issue.

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

"David Lozzi" <Da********@nospam.nospam> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Howdy,

I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders!

Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what I can tell, no one really knows what it means.... Here's my public function

Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As Integer) As String
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(p)
Dim iw As Integer = img.Width
Dim ih As Integer = img.Height
Dim nw, nh As Integer
Dim per As Decimal

If iw > w Or ih > h Then 'check to see if resize is necessary
If w > h Then 'get the larger dimension and get percentage
per = iw / w
Else
per = ih / h
End If

'create new sizes based on percentages
nw = iw * per
nh = ih * per

'now save it
Dim size As New Size(nw, nh)
Dim nimg As New Bitmap(img, size)
nimg.Save(p)

lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & nw & "x" & nh
Else
lblTemp.Text = "No resize necessary."
End If
End Function

and here is my code for the upload, abbreviated

If picType = "image/jpeg" Or picType = "image/gif" Or picType = "image/pjpeg" Or picType = "image/bmp" Then
File.PostedFile.SaveAs(path)
ResizeImage(path, 120, 95)
lblError.Text = "The speaker photo uploaded sucessfully! Make sure to click Update below to save changes."
Else
lblError.Text = "Invalid image format. Only JPG, JPEG, GIF and BMP images are allowed."
End If

Again, the upload works great

Thanks!!

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

Dec 9 '05 #2
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."

Dec 10 '05 #3
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.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Fri, 9 Dec 2005 11:15:17 -0500
| Lines: 270
| MIME-Version: 1.0
| Content-Type: multipart/alternative;
| boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:363864
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Besides the point, I just realized my math is wrong, please don't
critique. I just need to resolve the resize issue.
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| Howdy,
| I have a function that uploads an image and that works great. I love
.Nets built in upload, so much easier than 3rd party uploaders!
| Now I am making a public function that will take the path of the
uploaded image, and resize it with the provided dimensions. My function is
below. The current function is returning an error when run from the upload
function: A generic error occurred in GDI+. Not sure what exactly that
means. From what I can tell, no one really knows what it means.... Here's
my public function
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h
As Integer) As String
| Dim img As System.Drawing.Image
| img = System.Drawing.Image.FromFile(p)
| Dim iw As Integer = img.Width
| Dim ih As Integer = img.Height
| Dim nw, nh As Integer
| Dim per As Decimal
| If iw > w Or ih > h Then 'check to see if resize is necessary
| If w > h Then 'get the larger dimension and get percentage
| per = iw / w
| Else
| per = ih / h
| End If
| 'create new sizes based on percentages
| nw = iw * per
| nh = ih * per
| 'now save it
| Dim size As New Size(nw, nh)
| Dim nimg As New Bitmap(img, size)
| nimg.Save(p)
| lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " & nw
& "x" & nh
| Else
| lblTemp.Text = "No resize necessary."
| End If
| End Function
| and here is my code for the upload, abbreviated
| If picType = "image/jpeg" Or picType = "image/gif" Or
picType = "image/pjpeg" Or picType = "image/bmp" Then
| File.PostedFile.SaveAs(path)
| ResizeImage(path, 120, 95)
| lblError.Text = "The speaker photo uploaded
sucessfully! Make sure to click Update below to save changes."
| Else
| lblError.Text = "Invalid image format. Only JPG,
JPEG, GIF and BMP images are allowed."
| End If
| Again, the upload works great
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|

Dec 12 '05 #4
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

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:v1****************@TK2MSFTNGXA02.phx.gbl...
Hey David,

So this seems a pure GDI+ image processing issue. Does this problem occurs
only when the uploaded image is of certain format or is a common issue
that
will occur for any images that uploaded onto the server? Generally I think
we can throubleshoot through the following steps:

1. Make sure that the image is uploaded correctly onto the server , open
it
in image viewer to make sure the data is not corrupted.

2. Then, use some standard image resizeing code to resize the image...
here is some simple GDI+ code I picked from net which resize the image
through percentage value:

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Fri, 9 Dec 2005 11:15:17 -0500
| Lines: 270
| MIME-Version: 1.0
| Content-Type: multipart/alternative;
| boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:363864
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Besides the point, I just realized my math is wrong, please don't
critique. I just need to resolve the resize issue.
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| Howdy,
| I have a function that uploads an image and that works great. I love
Nets built in upload, so much easier than 3rd party uploaders!
| Now I am making a public function that will take the path of the
uploaded image, and resize it with the provided dimensions. My function is
below. The current function is returning an error when run from the upload
function: A generic error occurred in GDI+. Not sure what exactly that
means. From what I can tell, no one really knows what it means.... Here's
my public function
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal
h
As Integer) As String
| Dim img As System.Drawing.Image
| img = System.Drawing.Image.FromFile(p)
| Dim iw As Integer = img.Width
| Dim ih As Integer = img.Height
| Dim nw, nh As Integer
| Dim per As Decimal
| If iw > w Or ih > h Then 'check to see if resize is necessary
| If w > h Then 'get the larger dimension and get percentage
| per = iw / w
| Else
| per = ih / h
| End If
| 'create new sizes based on percentages
| nw = iw * per
| nh = ih * per
| 'now save it
| Dim size As New Size(nw, nh)
| Dim nimg As New Bitmap(img, size)
| nimg.Save(p)
| lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " &
nw
& "x" & nh
| Else
| lblTemp.Text = "No resize necessary."
| End If
| End Function
| and here is my code for the upload, abbreviated
| If picType = "image/jpeg" Or picType = "image/gif" Or
picType = "image/pjpeg" Or picType = "image/bmp" Then
| File.PostedFile.SaveAs(path)
| ResizeImage(path, 120, 95)
| lblError.Text = "The speaker photo uploaded
sucessfully! Make sure to click Update below to save changes."
| Else
| lblError.Text = "Invalid image format. Only JPG,
JPEG, GIF and BMP images are allowed."
| End If
| Again, the upload works great
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|

Dec 12 '05 #5
After reviewing your code, this function returns the resized image as an
image to the calling script. I need the resized image to be saved to disk.
My script is below. I get an error: Object reference not set to an instance
of an object, which pertains to the graphic object because when I remove
that chunk of code, I get the same GDI+ error.

Thanks!!

Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
Integer) As String
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(p)
Dim iw As Integer = img.Width
Dim ih As Integer = img.Height
Dim nw, nh As Integer
Dim per As Decimal

If iw > w Or ih > h Then 'check to see if resize is necessary
If iw > ih Then 'get the larger dimension and get percentage
per = w / iw
Else
per = h / ih
End If

'create new sizes based on percentages
nw = iw * per
nh = ih * per

'now save it
Dim photo As New Bitmap(nw, nh,
Imaging.PixelFormat.Format24bppRgb)
photo.SetResolution(img.HorizontalResolution,
img.VerticalResolution)
Dim graphic As Graphics
graphic.FromImage(photo)
Dim img2 As Image
graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)

graphic.Dispose()
photo.Save(p)

lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih &
"<br>New " & nw & "x" & nh
Else
lblTemp.Text = "No resize necessary."
End If
End Function

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

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:v1****************@TK2MSFTNGXA02.phx.gbl...
Hey David,

So this seems a pure GDI+ image processing issue. Does this problem occurs
only when the uploaded image is of certain format or is a common issue
that
will occur for any images that uploaded onto the server? Generally I think
we can throubleshoot through the following steps:

1. Make sure that the image is uploaded correctly onto the server , open
it
in image viewer to make sure the data is not corrupted.

2. Then, use some standard image resizeing code to resize the image...
here is some simple GDI+ code I picked from net which resize the image
through percentage value:

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Fri, 9 Dec 2005 11:15:17 -0500
| Lines: 270
| MIME-Version: 1.0
| Content-Type: multipart/alternative;
| boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:363864
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Besides the point, I just realized my math is wrong, please don't
critique. I just need to resolve the resize issue.
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| Howdy,
| I have a function that uploads an image and that works great. I love
Nets built in upload, so much easier than 3rd party uploaders!
| Now I am making a public function that will take the path of the
uploaded image, and resize it with the provided dimensions. My function is
below. The current function is returning an error when run from the upload
function: A generic error occurred in GDI+. Not sure what exactly that
means. From what I can tell, no one really knows what it means.... Here's
my public function
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal
h
As Integer) As String
| Dim img As System.Drawing.Image
| img = System.Drawing.Image.FromFile(p)
| Dim iw As Integer = img.Width
| Dim ih As Integer = img.Height
| Dim nw, nh As Integer
| Dim per As Decimal
| If iw > w Or ih > h Then 'check to see if resize is necessary
| If w > h Then 'get the larger dimension and get percentage
| per = iw / w
| Else
| per = ih / h
| End If
| 'create new sizes based on percentages
| nw = iw * per
| nh = ih * per
| 'now save it
| Dim size As New Size(nw, nh)
| Dim nimg As New Bitmap(img, size)
| nimg.Save(p)
| lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " &
nw
& "x" & nh
| Else
| lblTemp.Text = "No resize necessary."
| End If
| End Function
| and here is my code for the upload, abbreviated
| If picType = "image/jpeg" Or picType = "image/gif" Or
picType = "image/pjpeg" Or picType = "image/bmp" Then
| File.PostedFile.SaveAs(path)
| ResizeImage(path, 120, 95)
| lblError.Text = "The speaker photo uploaded
sucessfully! Make sure to click Update below to save changes."
| Else
| lblError.Text = "Invalid image format. Only JPG,
JPEG, GIF and BMP images are allowed."
| End If
| Again, the upload works great
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|

Dec 12 '05 #6
The object reference error is occuring on the DrawImage line, if that helps.

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

"David Lozzi" <Da********@nospam.nospam> wrote in message
news:O3**************@TK2MSFTNGP15.phx.gbl...
After reviewing your code, this function returns the resized image as an
image to the calling script. I need the resized image to be saved to disk.
My script is below. I get an error: Object reference not set to an
instance of an object, which pertains to the graphic object because when I
remove that chunk of code, I get the same GDI+ error.

Thanks!!

Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h As
Integer) As String
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(p)
Dim iw As Integer = img.Width
Dim ih As Integer = img.Height
Dim nw, nh As Integer
Dim per As Decimal

If iw > w Or ih > h Then 'check to see if resize is necessary
If iw > ih Then 'get the larger dimension and get percentage
per = w / iw
Else
per = h / ih
End If

'create new sizes based on percentages
nw = iw * per
nh = ih * per

'now save it
Dim photo As New Bitmap(nw, nh,
Imaging.PixelFormat.Format24bppRgb)
photo.SetResolution(img.HorizontalResolution,
img.VerticalResolution)
Dim graphic As Graphics
graphic.FromImage(photo)
Dim img2 As Image
graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)

graphic.Dispose()
photo.Save(p)

lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih &
"<br>New " & nw & "x" & nh
Else
lblTemp.Text = "No resize necessary."
End If
End Function

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

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:v1****************@TK2MSFTNGXA02.phx.gbl...
Hey David,

So this seems a pure GDI+ image processing issue. Does this problem
occurs
only when the uploaded image is of certain format or is a common issue
that
will occur for any images that uploaded onto the server? Generally I
think
we can throubleshoot through the following steps:

1. Make sure that the image is uploaded correctly onto the server , open
it
in image viewer to make sure the data is not corrupted.

2. Then, use some standard image resizeing code to resize the image...
here is some simple GDI+ code I picked from net which resize the image
through percentage value:

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Fri, 9 Dec 2005 11:15:17 -0500
| Lines: 270
| MIME-Version: 1.0
| Content-Type: multipart/alternative;
| boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:363864
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Besides the point, I just realized my math is wrong, please don't
critique. I just need to resolve the resize issue.
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| Howdy,
| I have a function that uploads an image and that works great. I love
Nets built in upload, so much easier than 3rd party uploaders!
| Now I am making a public function that will take the path of the
uploaded image, and resize it with the provided dimensions. My function
is
below. The current function is returning an error when run from the
upload
function: A generic error occurred in GDI+. Not sure what exactly that
means. From what I can tell, no one really knows what it means.... Here's
my public function
| Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal
h
As Integer) As String
| Dim img As System.Drawing.Image
| img = System.Drawing.Image.FromFile(p)
| Dim iw As Integer = img.Width
| Dim ih As Integer = img.Height
| Dim nw, nh As Integer
| Dim per As Decimal
| If iw > w Or ih > h Then 'check to see if resize is necessary
| If w > h Then 'get the larger dimension and get
percentage
| per = iw / w
| Else
| per = ih / h
| End If
| 'create new sizes based on percentages
| nw = iw * per
| nh = ih * per
| 'now save it
| Dim size As New Size(nw, nh)
| Dim nimg As New Bitmap(img, size)
| nimg.Save(p)
| lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New " &
nw
& "x" & nh
| Else
| lblTemp.Text = "No resize necessary."
| End If
| End Function
| and here is my code for the upload, abbreviated
| If picType = "image/jpeg" Or picType = "image/gif" Or
picType = "image/pjpeg" Or picType = "image/bmp" Then
| File.PostedFile.SaveAs(path)
| ResizeImage(path, 120, 95)
| lblError.Text = "The speaker photo uploaded
sucessfully! Make sure to click Update below to save changes."
| Else
| lblError.Text = "Invalid image format. Only JPG,
JPEG, GIF and BMP images are allowed."
| End If
| Again, the upload works great
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|


Dec 12 '05 #7
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Mon, 12 Dec 2005 09:40:02 -0500
| Lines: 229
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364219
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| The object reference error is occuring on the DrawImage line, if that
helps.
|
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|
|
| "David Lozzi" <Da********@nospam.nospam> wrote in message
| news:O3**************@TK2MSFTNGP15.phx.gbl...
| > After reviewing your code, this function returns the resized image as
an
| > image to the calling script. I need the resized image to be saved to
disk.
| > My script is below. I get an error: Object reference not set to an
| > instance of an object, which pertains to the graphic object because
when I
| > remove that chunk of code, I get the same GDI+ error.
| >
| > Thanks!!
| >
| > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h
As
| > Integer) As String
| > Dim img As System.Drawing.Image
| > img = System.Drawing.Image.FromFile(p)
| > Dim iw As Integer = img.Width
| > Dim ih As Integer = img.Height
| > Dim nw, nh As Integer
| > Dim per As Decimal
| >
| > If iw > w Or ih > h Then 'check to see if resize is necessary
| > If iw > ih Then 'get the larger dimension and get percentage
| > per = w / iw
| > Else
| > per = h / ih
| > End If
| >
| > 'create new sizes based on percentages
| > nw = iw * per
| > nh = ih * per
| >
| > 'now save it
| > Dim photo As New Bitmap(nw, nh,
| > Imaging.PixelFormat.Format24bppRgb)
| > photo.SetResolution(img.HorizontalResolution,
| > img.VerticalResolution)
| > Dim graphic As Graphics
| > graphic.FromImage(photo)
| > Dim img2 As Image
| > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| >
| > graphic.Dispose()
| > photo.Save(p)
| >
| > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih
&
| > "<br>New " & nw & "x" & nh
| > Else
| > lblTemp.Text = "No resize necessary."
| > End If
| > End Function
| >
| > --
| > David Lozzi
| > Web Applications Developer
| > dlozzi@(remove-this)delphi-ts.com
| >
| >
| >
| > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| >> Hey David,
| >>
| >> So this seems a pure GDI+ image processing issue. Does this problem
| >> occurs
| >> only when the uploaded image is of certain format or is a common issue
| >> that
| >> will occur for any images that uploaded onto the server? Generally I
| >> think
| >> we can throubleshoot through the following steps:
| >>
| >> 1. Make sure that the image is uploaded correctly onto the server ,
open
| >> it
| >> in image viewer to make sure the data is not corrupted.
| >>
| >> 2. Then, use some standard image resizeing code to resize the image...
| >> here is some simple GDI+ code I picked from net which resize the image
| >> through percentage value:
| >>
| >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| >> {
| >> float nPercent = ((float)Percent/100);
| >>
| >> int sourceWidth = imgPhoto.Width;
| >> int sourceHeight = imgPhoto.Height;
| >> int sourceX = 0;
| >> int sourceY = 0;
| >>
| >> int destX = 0;
| >> int destY = 0;
| >> int destWidth = (int)(sourceWidth * nPercent);
| >> int destHeight = (int)(sourceHeight * nPercent);
| >>
| >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| >> PixelFormat.Format24bppRgb);
| >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| >> 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" <Da********@nospam.nospam>
| >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| >> | Subject: Re: Upload image and then resize it - A generic error
occurred
| >> in GDI+.
| >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| >> | Lines: 270
| >> | MIME-Version: 1.0
| >> | Content-Type: multipart/alternative;
| >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| >> | X-Priority: 3
| >> | X-MSMail-Priority: Normal
| >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| >> | Xref: TK2MSFTNGXA02.phx.gbl
| >> microsoft.public.dotnet.framework.aspnet:363864
| >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| >> |
| >> | Besides the point, I just realized my math is wrong, please don't
| >> critique. I just need to resolve the resize issue.
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| >> | Howdy,
| >> | I have a function that uploads an image and that works great. I
love
| >> Nets built in upload, so much easier than 3rd party uploaders!
| >> | Now I am making a public function that will take the path of the
| >> uploaded image, and resize it with the provided dimensions. My
function
| >> is
| >> below. The current function is returning an error when run from the
| >> upload
| >> function: A generic error occurred in GDI+. Not sure what exactly that
| >> means. From what I can tell, no one really knows what it means....
Here's
| >> my public function
| >> | Function ResizeImage(ByVal p As String, ByVal w As Integer,
ByVal
| >> h
| >> As Integer) As String
| >> | Dim img As System.Drawing.Image
| >> | img = System.Drawing.Image.FromFile(p)
| >> | Dim iw As Integer = img.Width
| >> | Dim ih As Integer = img.Height
| >> | Dim nw, nh As Integer
| >> | Dim per As Decimal
| >> | If iw > w Or ih > h Then 'check to see if resize is
necessary
| >> | If w > h Then 'get the larger dimension and get
| >> percentage
| >> | per = iw / w
| >> | Else
| >> | per = ih / h
| >> | End If
| >> | 'create new sizes based on percentages
| >> | nw = iw * per
| >> | nh = ih * per
| >> | 'now save it
| >> | Dim size As New Size(nw, nh)
| >> | Dim nimg As New Bitmap(img, size)
| >> | nimg.Save(p)
| >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New "
&
| >> nw
| >> & "x" & nh
| >> | Else
| >> | lblTemp.Text = "No resize necessary."
| >> | End If
| >> | End Function
| >> | and here is my code for the upload, abbreviated
| >> | If picType = "image/jpeg" Or picType = "image/gif"
Or
| >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| >> | File.PostedFile.SaveAs(path)
| >> | ResizeImage(path, 120, 95)
| >> | lblError.Text = "The speaker photo uploaded
| >> sucessfully! Make sure to click Update below to save changes."
| >> | Else
| >> | lblError.Text = "Invalid image format. Only
JPG,
| >> JPEG, GIF and BMP images are allowed."
| >> | End If
| >> | Again, the upload works great
| >> | Thanks!!
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> |
| >> |
| >>
| >
| >
|
|
|

Dec 13 '05 #8
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: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Tue, 13 Dec 2005 02:45:03 GMT
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <Mh**************@TK2MSFTNGXA02.phx.gbl>
| 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" <Da********@nospam.nospam>
| | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| <#G**************@TK2MSFTNGP09.phx.gbl>
| <v1**************@TK2MSFTNGXA02.phx.gbl>
| <O3**************@TK2MSFTNGP15.phx.gbl>
| | Subject: Re: Upload image and then resize it - A generic error occurred
| in GDI+.
| | Date: Mon, 12 Dec 2005 09:40:02 -0500
| | Lines: 229
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| | X-RFC2646: Format=Flowed; Response
| | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet:364219
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | The object reference error is occuring on the DrawImage line, if that
| helps.
| |
| | --
| | David Lozzi
| | Web Applications Developer
| | dlozzi@(remove-this)delphi-ts.com
| |
| |
| |
| | "David Lozzi" <Da********@nospam.nospam> wrote in message
| | news:O3**************@TK2MSFTNGP15.phx.gbl...
| | > After reviewing your code, this function returns the resized image as
| an
| | > image to the calling script. I need the resized image to be saved to
| disk.
| | > My script is below. I get an error: Object reference not set to an
| | > instance of an object, which pertains to the graphic object because
| when I
| | > remove that chunk of code, I get the same GDI+ error.
| | >
| | > Thanks!!
| | >
| | > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal
h
| As
| | > Integer) As String
| | > Dim img As System.Drawing.Image
| | > img = System.Drawing.Image.FromFile(p)
| | > Dim iw As Integer = img.Width
| | > Dim ih As Integer = img.Height
| | > Dim nw, nh As Integer
| | > Dim per As Decimal
| | >
| | > If iw > w Or ih > h Then 'check to see if resize is necessary
| | > If iw > ih Then 'get the larger dimension and get
percentage
| | > per = w / iw
| | > Else
| | > per = h / ih
| | > End If
| | >
| | > 'create new sizes based on percentages
| | > nw = iw * per
| | > nh = ih * per
| | >
| | > 'now save it
| | > Dim photo As New Bitmap(nw, nh,
| | > Imaging.PixelFormat.Format24bppRgb)
| | > photo.SetResolution(img.HorizontalResolution,
| | > img.VerticalResolution)
| | > Dim graphic As Graphics
| | > graphic.FromImage(photo)
| | > Dim img2 As Image
| | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| | >
| | > graphic.Dispose()
| | > photo.Save(p)
| | >
| | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" &
ih
| &
| | > "<br>New " & nw & "x" & nh
| | > Else
| | > lblTemp.Text = "No resize necessary."
| | > End If
| | > End Function
| | >
| | > --
| | > David Lozzi
| | > Web Applications Developer
| | > dlozzi@(remove-this)delphi-ts.com
| | >
| | >
| | >
| | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| | >> Hey David,
| | >>
| | >> So this seems a pure GDI+ image processing issue. Does this problem
| | >> occurs
| | >> only when the uploaded image is of certain format or is a common
issue
| | >> that
| | >> will occur for any images that uploaded onto the server? Generally I
| | >> think
| | >> we can throubleshoot through the following steps:
| | >>
| | >> 1. Make sure that the image is uploaded correctly onto the server ,
| open
| | >> it
| | >> in image viewer to make sure the data is not corrupted.
| | >>
| | >> 2. Then, use some standard image resizeing code to resize the
image...
| | >> here is some simple GDI+ code I picked from net which resize the
image
| | >> through percentage value:
| | >>
| | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| | >> {
| | >> float nPercent = ((float)Percent/100);
| | >>
| | >> int sourceWidth = imgPhoto.Width;
| | >> int sourceHeight = imgPhoto.Height;
| | >> int sourceX = 0;
| | >> int sourceY = 0;
| | >>
| | >> int destX = 0;
| | >> int destY = 0;
| | >> int destWidth = (int)(sourceWidth * nPercent);
| | >> int destHeight = (int)(sourceHeight * nPercent);
| | >>
| | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| | >> PixelFormat.Format24bppRgb);
| | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| | >> 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" <Da********@nospam.nospam>
| | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| | >> | Subject: Re: Upload image and then resize it - A generic error
| occurred
| | >> in GDI+.
| | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| | >> | Lines: 270
| | >> | MIME-Version: 1.0
| | >> | Content-Type: multipart/alternative;
| | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| | >> | X-Priority: 3
| | >> | X-MSMail-Priority: Normal
| | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| | >> | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| | >> | Xref: TK2MSFTNGXA02.phx.gbl
| | >> microsoft.public.dotnet.framework.aspnet:363864
| | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| | >> |
| | >> | Besides the point, I just realized my math is wrong, please don't
| | >> critique. I just need to resolve the resize issue.
| | >> | --
| | >> | David Lozzi
| | >> | Web Applications Developer
| | >> | dlozzi@(remove-this)delphi-ts.com
| | >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| | >> | Howdy,
| | >> | I have a function that uploads an image and that works great. I
| love
| | >> Nets built in upload, so much easier than 3rd party uploaders!
| | >> | Now I am making a public function that will take the path of the
| | >> uploaded image, and resize it with the provided dimensions. My
| function
| | >> is
| | >> below. The current function is returning an error when run from the
| | >> upload
| | >> function: A generic error occurred in GDI+. Not sure what exactly
that
| | >> means. From what I can tell, no one really knows what it means....
| Here's
| | >> my public function
| | >> | Function ResizeImage(ByVal p As String, ByVal w As Integer,
| ByVal
| | >> h
| | >> As Integer) As String
| | >> | Dim img As System.Drawing.Image
| | >> | img = System.Drawing.Image.FromFile(p)
| | >> | Dim iw As Integer = img.Width
| | >> | Dim ih As Integer = img.Height
| | >> | Dim nw, nh As Integer
| | >> | Dim per As Decimal
| | >> | If iw > w Or ih > h Then 'check to see if resize is
| necessary
| | >> | If w > h Then 'get the larger dimension and get
| | >> percentage
| | >> | per = iw / w
| | >> | Else
| | >> | per = ih / h
| | >> | End If
| | >> | 'create new sizes based on percentages
| | >> | nw = iw * per
| | >> | nh = ih * per
| | >> | 'now save it
| | >> | Dim size As New Size(nw, nh)
| | >> | Dim nimg As New Bitmap(img, size)
| | >> | nimg.Save(p)
| | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New
"
| &
| | >> nw
| | >> & "x" & nh
| | >> | Else
| | >> | lblTemp.Text = "No resize necessary."
| | >> | End If
| | >> | End Function
| | >> | and here is my code for the upload, abbreviated
| | >> | If picType = "image/jpeg" Or picType =
"image/gif"
| Or
| | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| | >> | File.PostedFile.SaveAs(path)
| | >> | ResizeImage(path, 120, 95)
| | >> | lblError.Text = "The speaker photo uploaded
| | >> sucessfully! Make sure to click Update below to save changes."
| | >> | Else
| | >> | lblError.Text = "Invalid image format. Only
| JPG,
| | >> JPEG, GIF and BMP images are allowed."
| | >> | End If
| | >> | Again, the upload works great
| | >> | Thanks!!
| | >> | --
| | >> | David Lozzi
| | >> | Web Applications Developer
| | >> | dlozzi@(remove-this)delphi-ts.com
| | >> |
| | >> |
| | >>
| | >
| | >
| |
| |
| |
|
|

Dec 15 '05 #9
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


"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message news:Mh**************@TK2MSFTNGXA02.phx.gbl...
Thanks for your response David,

I think the NullReference exception is due to the graphic object is not
correctly assigned. From the code you pasted:

=================
Dim graphic As Graphics
graphic.FromImage(photo)
Dim img2 As Image
graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)

graphic.Dispose()
photo.Save(p)
=================

You use graphic.FromImage(photo), this will return an Graphics object
instance, and you need to assign it to the Graphics reference, like:


........
Dim graphic As Graphics
graphic = Graphics.FromImage(photo)
............


Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "David Lozzi" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Mon, 12 Dec 2005 09:40:02 -0500
| Lines: 229
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364219
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| The object reference error is occuring on the DrawImage line, if that
helps.
|
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|
|
| "David Lozzi" <Da********@nospam.nospam> wrote in message
| news:O3**************@TK2MSFTNGP15.phx.gbl...
| > After reviewing your code, this function returns the resized image as
an
| > image to the calling script. I need the resized image to be saved to
disk.
| > My script is below. I get an error: Object reference not set to an
| > instance of an object, which pertains to the graphic object because
when I
| > remove that chunk of code, I get the same GDI+ error.
| >
| > Thanks!!
| >
| > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h
As
| > Integer) As String
| > Dim img As System.Drawing.Image
| > img = System.Drawing.Image.FromFile(p)
| > Dim iw As Integer = img.Width
| > Dim ih As Integer = img.Height
| > Dim nw, nh As Integer
| > Dim per As Decimal
| >
| > If iw > w Or ih > h Then 'check to see if resize is necessary
| > If iw > ih Then 'get the larger dimension and get percentage
| > per = w / iw
| > Else
| > per = h / ih
| > End If
| >
| > 'create new sizes based on percentages
| > nw = iw * per
| > nh = ih * per
| >
| > 'now save it
| > Dim photo As New Bitmap(nw, nh,
| > Imaging.PixelFormat.Format24bppRgb)
| > photo.SetResolution(img.HorizontalResolution,
| > img.VerticalResolution)
| > Dim graphic As Graphics
| > graphic.FromImage(photo)
| > Dim img2 As Image
| > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| >
| > graphic.Dispose()
| > photo.Save(p)
| >
| > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih
&
| > "<br>New " & nw & "x" & nh
| > Else
| > lblTemp.Text = "No resize necessary."
| > End If
| > End Function
| >
| > --
| > David Lozzi
| > Web Applications Developer
| > dlozzi@(remove-this)delphi-ts.com
| >
| >
| >
| > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| >> Hey David,
| >>
| >> So this seems a pure GDI+ image processing issue. Does this problem
| >> occurs
| >> only when the uploaded image is of certain format or is a common issue
| >> that
| >> will occur for any images that uploaded onto the server? Generally I
| >> think
| >> we can throubleshoot through the following steps:
| >>
| >> 1. Make sure that the image is uploaded correctly onto the server ,
open
| >> it
| >> in image viewer to make sure the data is not corrupted.
| >>
| >> 2. Then, use some standard image resizeing code to resize the image...
| >> here is some simple GDI+ code I picked from net which resize the image
| >> through percentage value:
| >>
| >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| >> {
| >> float nPercent = ((float)Percent/100);
| >>
| >> int sourceWidth = imgPhoto.Width;
| >> int sourceHeight = imgPhoto.Height;
| >> int sourceX = 0;
| >> int sourceY = 0;
| >>
| >> int destX = 0;
| >> int destY = 0;
| >> int destWidth = (int)(sourceWidth * nPercent);
| >> int destHeight = (int)(sourceHeight * nPercent);
| >>
| >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| >> PixelFormat.Format24bppRgb);
| >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| >> 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" <Da********@nospam.nospam>
| >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| >> | Subject: Re: Upload image and then resize it - A generic error
occurred
| >> in GDI+.
| >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| >> | Lines: 270
| >> | MIME-Version: 1.0
| >> | Content-Type: multipart/alternative;
| >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| >> | X-Priority: 3
| >> | X-MSMail-Priority: Normal
| >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| >> | Xref: TK2MSFTNGXA02.phx.gbl
| >> microsoft.public.dotnet.framework.aspnet:363864
| >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| >> |
| >> | Besides the point, I just realized my math is wrong, please don't
| >> critique. I just need to resolve the resize issue.
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| >> | Howdy,
| >> | I have a function that uploads an image and that works great. I
love
| >> Nets built in upload, so much easier than 3rd party uploaders!
| >> | Now I am making a public function that will take the path of the
| >> uploaded image, and resize it with the provided dimensions. My
function
| >> is
| >> below. The current function is returning an error when run from the
| >> upload
| >> function: A generic error occurred in GDI+. Not sure what exactly that
| >> means. From what I can tell, no one really knows what it means....
Here's
| >> my public function
| >> | Function ResizeImage(ByVal p As String, ByVal w As Integer,
ByVal
| >> h
| >> As Integer) As String
| >> | Dim img As System.Drawing.Image
| >> | img = System.Drawing.Image.FromFile(p)
| >> | Dim iw As Integer = img.Width
| >> | Dim ih As Integer = img.Height
| >> | Dim nw, nh As Integer
| >> | Dim per As Decimal
| >> | If iw > w Or ih > h Then 'check to see if resize is
necessary
| >> | If w > h Then 'get the larger dimension and get
| >> percentage
| >> | per = iw / w
| >> | Else
| >> | per = ih / h
| >> | End If
| >> | 'create new sizes based on percentages
| >> | nw = iw * per
| >> | nh = ih * per
| >> | 'now save it
| >> | Dim size As New Size(nw, nh)
| >> | Dim nimg As New Bitmap(img, size)
| >> | nimg.Save(p)
| >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New "
&
| >> nw
| >> & "x" & nh
| >> | Else
| >> | lblTemp.Text = "No resize necessary."
| >> | End If
| >> | End Function
| >> | and here is my code for the upload, abbreviated
| >> | If picType = "image/jpeg" Or picType = "image/gif"
Or
| >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| >> | File.PostedFile.SaveAs(path)
| >> | ResizeImage(path, 120, 95)
| >> | lblError.Text = "The speaker photo uploaded
| >> sucessfully! Make sure to click Update below to save changes."
| >> | Else
| >> | lblError.Text = "Invalid image format. Only
JPG,
| >> JPEG, GIF and BMP images are allowed."
| >> | End If
| >> | Again, the upload works great
| >> | Thanks!!
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> |
| >> |
| >>
| >
| >
|
|
|

Dec 15 '05 #10
If I change the path to a static path like c:\photos\image.gif it writes an image file, however the image is black, but appears to be to the correct dimensions.

Thanks,

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

"David Lozzi" <Da********@nospam.nospam> wrote in message news:uf**************@TK2MSFTNGP10.phx.gbl...
I updated the script a bit and also included your last post. Here's my current script which is still receiving the GDI+ genereic error. I removed the Try statement this code was in and it appears to be erroring on the line with the *

Dim img As Image

img = Image.FromFile(p)

Dim photo As New Bitmap(img)

Dim photo2 As New Bitmap(img.HorizontalResolution, img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)

Dim grPhoto As Graphics

grPhoto = Graphics.FromImage(photo)

grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))

grPhoto.Dispose()

photo.Dispose()

photo2.SetResolution(nw, nh)

photo2.Save(p) *

photo2.Dispose()
Thanks!!

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


"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message news:Mh**************@TK2MSFTNGXA02.phx.gbl...
Thanks for your response David,

I think the NullReference exception is due to the graphic object is not
correctly assigned. From the code you pasted:

=================
Dim graphic As Graphics
graphic.FromImage(photo)
Dim img2 As Image
graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)

graphic.Dispose()
photo.Save(p)
=================

You use graphic.FromImage(photo), this will return an Graphics object
instance, and you need to assign it to the Graphics reference, like:


........
Dim graphic As Graphics
graphic = Graphics.FromImage(photo)
............


Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "David Lozzi" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
| Subject: Re: Upload image and then resize it - A generic error occurred
in GDI+.
| Date: Mon, 12 Dec 2005 09:40:02 -0500
| Lines: 229
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364219
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| The object reference error is occuring on the DrawImage line, if that
helps.
|
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|
|
| "David Lozzi" <Da********@nospam.nospam> wrote in message
| news:O3**************@TK2MSFTNGP15.phx.gbl...
| > After reviewing your code, this function returns the resized image as
an
| > image to the calling script. I need the resized image to be saved to
disk.
| > My script is below. I get an error: Object reference not set to an
| > instance of an object, which pertains to the graphic object because
when I
| > remove that chunk of code, I get the same GDI+ error.
| >
| > Thanks!!
| >
| > Function ResizeImage(ByVal p As String, ByVal w As Integer, ByVal h
As
| > Integer) As String
| > Dim img As System.Drawing.Image
| > img = System.Drawing.Image.FromFile(p)
| > Dim iw As Integer = img.Width
| > Dim ih As Integer = img.Height
| > Dim nw, nh As Integer
| > Dim per As Decimal
| >
| > If iw > w Or ih > h Then 'check to see if resize is necessary
| > If iw > ih Then 'get the larger dimension and get percentage
| > per = w / iw
| > Else
| > per = h / ih
| > End If
| >
| > 'create new sizes based on percentages
| > nw = iw * per
| > nh = ih * per
| >
| > 'now save it
| > Dim photo As New Bitmap(nw, nh,
| > Imaging.PixelFormat.Format24bppRgb)
| > photo.SetResolution(img.HorizontalResolution,
| > img.VerticalResolution)
| > Dim graphic As Graphics
| > graphic.FromImage(photo)
| > Dim img2 As Image
| > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| >
| > graphic.Dispose()
| > photo.Save(p)
| >
| > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw & "x" & ih
&
| > "<br>New " & nw & "x" & nh
| > Else
| > lblTemp.Text = "No resize necessary."
| > End If
| > End Function
| >
| > --
| > David Lozzi
| > Web Applications Developer
| > dlozzi@(remove-this)delphi-ts.com
| >
| >
| >
| > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| >> Hey David,
| >>
| >> So this seems a pure GDI+ image processing issue. Does this problem
| >> occurs
| >> only when the uploaded image is of certain format or is a common issue
| >> that
| >> will occur for any images that uploaded onto the server? Generally I
| >> think
| >> we can throubleshoot through the following steps:
| >>
| >> 1. Make sure that the image is uploaded correctly onto the server ,
open
| >> it
| >> in image viewer to make sure the data is not corrupted.
| >>
| >> 2. Then, use some standard image resizeing code to resize the image...
| >> here is some simple GDI+ code I picked from net which resize the image
| >> through percentage value:
| >>
| >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| >> {
| >> float nPercent = ((float)Percent/100);
| >>
| >> int sourceWidth = imgPhoto.Width;
| >> int sourceHeight = imgPhoto.Height;
| >> int sourceX = 0;
| >> int sourceY = 0;
| >>
| >> int destX = 0;
| >> int destY = 0;
| >> int destWidth = (int)(sourceWidth * nPercent);
| >> int destHeight = (int)(sourceHeight * nPercent);
| >>
| >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| >> PixelFormat.Format24bppRgb);
| >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| >> 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" <Da********@nospam.nospam>
| >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| >> | Subject: Re: Upload image and then resize it - A generic error
occurred
| >> in GDI+.
| >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| >> | Lines: 270
| >> | MIME-Version: 1.0
| >> | Content-Type: multipart/alternative;
| >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| >> | X-Priority: 3
| >> | X-MSMail-Priority: Normal
| >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| >> | Xref: TK2MSFTNGXA02.phx.gbl
| >> microsoft.public.dotnet.framework.aspnet:363864
| >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| >> |
| >> | Besides the point, I just realized my math is wrong, please don't
| >> critique. I just need to resolve the resize issue.
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| >> | Howdy,
| >> | I have a function that uploads an image and that works great. I
love
| >> Nets built in upload, so much easier than 3rd party uploaders!
| >> | Now I am making a public function that will take the path of the
| >> uploaded image, and resize it with the provided dimensions. My
function
| >> is
| >> below. The current function is returning an error when run from the
| >> upload
| >> function: A generic error occurred in GDI+. Not sure what exactly that
| >> means. From what I can tell, no one really knows what it means....
Here's
| >> my public function
| >> | Function ResizeImage(ByVal p As String, ByVal w As Integer,
ByVal
| >> h
| >> As Integer) As String
| >> | Dim img As System.Drawing.Image
| >> | img = System.Drawing.Image.FromFile(p)
| >> | Dim iw As Integer = img.Width
| >> | Dim ih As Integer = img.Height
| >> | Dim nw, nh As Integer
| >> | Dim per As Decimal
| >> | If iw > w Or ih > h Then 'check to see if resize is
necessary
| >> | If w > h Then 'get the larger dimension and get
| >> percentage
| >> | per = iw / w
| >> | Else
| >> | per = ih / h
| >> | End If
| >> | 'create new sizes based on percentages
| >> | nw = iw * per
| >> | nh = ih * per
| >> | 'now save it
| >> | Dim size As New Size(nw, nh)
| >> | Dim nimg As New Bitmap(img, size)
| >> | nimg.Save(p)
| >> | lblTemp.Text = "<br>Old " & iw & "x" & ih & "<br>New "
&
| >> nw
| >> & "x" & nh
| >> | Else
| >> | lblTemp.Text = "No resize necessary."
| >> | End If
| >> | End Function
| >> | and here is my code for the upload, abbreviated
| >> | If picType = "image/jpeg" Or picType = "image/gif"
Or
| >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| >> | File.PostedFile.SaveAs(path)
| >> | ResizeImage(path, 120, 95)
| >> | lblError.Text = "The speaker photo uploaded
| >> sucessfully! Make sure to click Update below to save changes."
| >> | Else
| >> | lblError.Text = "Invalid image format. Only
JPG,
| >> JPEG, GIF and BMP images are allowed."
| >> | End If
| >> | Again, the upload works great
| >> | Thanks!!
| >> | --
| >> | David Lozzi
| >> | Web Applications Developer
| >> | dlozzi@(remove-this)delphi-ts.com
| >> |
| >> |
| >>
| >
| >
|
|
|

Dec 15 '05 #11
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.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
<Mh**************@TK2MSFTNGXA02.phx.gbl>
<uf**************@TK2MSFTNGP10.phx.gbl>
| 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: <ei**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
| I updated the script a bit and also included your last post. Here's my
current script which is still receiving the GDI+ genereic error. I removed
the Try statement this code was in and it appears to be erroring on the
line with the *
| Dim img As Image
| img = Image.FromFile(p)
| Dim photo As New Bitmap(img)
| Dim photo2 As New Bitmap(img.HorizontalResolution,
img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)
| Dim grPhoto As Graphics
| grPhoto = Graphics.FromImage(photo)
| grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))
| grPhoto.Dispose()
| photo.Dispose()
| photo2.SetResolution(nw, nh)
| photo2.Save(p) *
| photo2.Dispose()
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:Mh**************@TK2MSFTNGXA02.phx.gbl...
| > Thanks for your response David,
| >
| > I think the NullReference exception is due to the graphic object is
not
| > correctly assigned. From the code you pasted:
| >
| > =================
| > Dim graphic As Graphics
| > graphic.FromImage(photo)
| > Dim img2 As Image
| > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| >
| > graphic.Dispose()
| > photo.Save(p)
| > =================
| >
| > You use graphic.FromImage(photo), this will return an Graphics object
| > instance, and you need to assign it to the Graphics reference, like:
| >
| >
| > ........
| > Dim graphic As Graphics
| > graphic = Graphics.FromImage(photo)
| > ............
| >
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "David Lozzi" <Da********@nospam.nospam>
| > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > <#G**************@TK2MSFTNGP09.phx.gbl>
| > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | Subject: Re: Upload image and then resize it - A generic error
occurred
| > in GDI+.
| > | Date: Mon, 12 Dec 2005 09:40:02 -0500
| > | Lines: 229
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Response
| > | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:364219
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | The object reference error is occuring on the DrawImage line, if
that
| > helps.
| > |
| > | --
| > | David Lozzi
| > | Web Applications Developer
| > | dlozzi@(remove-this)delphi-ts.com
| > |
| > |
| > |
| > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | news:O3**************@TK2MSFTNGP15.phx.gbl...
| > | > After reviewing your code, this function returns the resized
image as
| > an
| > | > image to the calling script. I need the resized image to be saved
to
| > disk.
| > | > My script is below. I get an error: Object reference not set to
an
| > | > instance of an object, which pertains to the graphic object
because
| > when I
| > | > remove that chunk of code, I get the same GDI+ error.
| > | >
| > | > Thanks!!
| > | >
| > | > Function ResizeImage(ByVal p As String, ByVal w As Integer,
ByVal h
| > As
| > | > Integer) As String
| > | > Dim img As System.Drawing.Image
| > | > img = System.Drawing.Image.FromFile(p)
| > | > Dim iw As Integer = img.Width
| > | > Dim ih As Integer = img.Height
| > | > Dim nw, nh As Integer
| > | > Dim per As Decimal
| > | >
| > | > If iw > w Or ih > h Then 'check to see if resize is
necessary
| > | > If iw > ih Then 'get the larger dimension and get
percentage
| > | > per = w / iw
| > | > Else
| > | > per = h / ih
| > | > End If
| > | >
| > | > 'create new sizes based on percentages
| > | > nw = iw * per
| > | > nh = ih * per
| > | >
| > | > 'now save it
| > | > Dim photo As New Bitmap(nw, nh,
| > | > Imaging.PixelFormat.Format24bppRgb)
| > | > photo.SetResolution(img.HorizontalResolution,
| > | > img.VerticalResolution)
| > | > Dim graphic As Graphics
| > | > graphic.FromImage(photo)
| > | > Dim img2 As Image
| > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh),
New
| > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | >
| > | > graphic.Dispose()
| > | > photo.Save(p)
| > | >
| > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw &
"x" & ih
| > &
| > | > "<br>New " & nw & "x" & nh
| > | > Else
| > | > lblTemp.Text = "No resize necessary."
| > | > End If
| > | > End Function
| > | >
| > | > --
| > | > David Lozzi
| > | > Web Applications Developer
| > | > dlozzi@(remove-this)delphi-ts.com
| > | >
| > | >
| > | >
| > | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
message
| > | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| > | >> Hey David,
| > | >>
| > | >> So this seems a pure GDI+ image processing issue. Does this
problem
| > | >> occurs
| > | >> only when the uploaded image is of certain format or is a common
issue
| > | >> that
| > | >> will occur for any images that uploaded onto the server?
Generally I
| > | >> think
| > | >> we can throubleshoot through the following steps:
| > | >>
| > | >> 1. Make sure that the image is uploaded correctly onto the
server ,
| > open
| > | >> it
| > | >> in image viewer to make sure the data is not corrupted.
| > | >>
| > | >> 2. Then, use some standard image resizeing code to resize the
image...
| > | >> here is some simple GDI+ code I picked from net which resize the
image
| > | >> through percentage value:
| > | >>
| > | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| > | >> {
| > | >> float nPercent = ((float)Percent/100);
| > | >>
| > | >> int sourceWidth = imgPhoto.Width;
| > | >> int sourceHeight = imgPhoto.Height;
| > | >> int sourceX = 0;
| > | >> int sourceY = 0;
| > | >>
| > | >> int destX = 0;
| > | >> int destY = 0;
| > | >> int destWidth = (int)(sourceWidth * nPercent);
| > | >> int destHeight = (int)(sourceHeight * nPercent);
| > | >>
| > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| > | >> PixelFormat.Format24bppRgb);
| > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| > | >> 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" <Da********@nospam.nospam>
| > | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | >> | Subject: Re: Upload image and then resize it - A generic error
| > occurred
| > | >> in GDI+.
| > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| > | >> | Lines: 270
| > | >> | MIME-Version: 1.0
| > | >> | Content-Type: multipart/alternative;
| > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| > | >> | X-Priority: 3
| > | >> | X-MSMail-Priority: Normal
| > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
24.63.42.200
| > | >> | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | >> | Xref: TK2MSFTNGXA02.phx.gbl
| > | >> microsoft.public.dotnet.framework.aspnet:363864
| > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | >> |
| > | >> | Besides the point, I just realized my math is wrong, please
don't
| > | >> critique. I just need to resolve the resize issue.
| > | >> | --
| > | >> | David Lozzi
| > | >> | Web Applications Developer
| > | >> | dlozzi@(remove-this)delphi-ts.com
| > | >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | >> | Howdy,
| > | >> | I have a function that uploads an image and that works
great. I
| > love
| > | >> Nets built in upload, so much easier than 3rd party uploaders!
| > | >> | Now I am making a public function that will take the path of
the
| >| >> uploaded image, and resize it with the provided dimensions. My
| > function
| > | >> is
| > | >> below. The current function is returning an error when run from
the
| > | >> upload
| > | >> function: A generic error occurred in GDI+. Not sure what
exactly that
| > | >> means. From what I can tell, no one really knows what it
means....
| > Here's
| > | >> my public function
| > | >> | Function ResizeImage(ByVal p As String, ByVal w As
Integer,
| > ByVal
| > | >> h
| > | >> As Integer) As String
| > | >> | Dim img As System.Drawing.Image
| > | >> | img = System.Drawing.Image.FromFile(p)
| > | >> | Dim iw As Integer = img.Width
| > | >> | Dim ih As Integer = img.Height
| > | >> | Dim nw, nh As Integer
| > | >> | Dim per As Decimal
| > | >> | If iw > w Or ih > h Then 'check to see if resize is
necessary | > | >> | If w > h Then 'get the larger dimension and get | >> percentage

| > | >> | per = iw / w
| > | >> | Else
| > | >> | per = ih / h
| > | >> | End If
| > | >> | 'create new sizes based on percentages
| > | >> | nw = iw * per
| > | >> | nh = ih * per
| > | >> | 'now save it
| > | >> | Dim size As New Size(nw, nh)
| > | >> | Dim nimg As New Bitmap(img, size)
| > | >> | nimg.Save(p)
| > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih &
"<br>New "
| > &
| > | >> nw
| > | >> & "x" & nh
| > | >> | Else
| > | >> | lblTemp.Text = "No resize necessary."
| > | >> | End If
| > | >> | End Function
| > | >> | and here is my code for the upload, abbreviated
| > | >> | If picType = "image/jpeg" Or picType =
"image/gif"
| > Or
| > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| > | >> | File.PostedFile.SaveAs(path)
| > | >> | ResizeImage(path, 120, 95)
| > | >> | lblError.Text = "The speaker photo
uploaded
| > | >> sucessfully! Make sure to click Update below to save changes."
| > | >> | Else
| > | >> | lblError.Text = "Invalid image format.
Only
| > JPG,
| > | >> JPEG, GIF and BMP images are allowed."
| > | >> | End If
| > | >> | Again, the upload works great
| > | >> | Thanks!!
| > | >> | --
| > | >> | David Lozzi
| > | >> | Web Applications Developer
| > | >> | dlozzi@(remove-this)delphi-ts.com
| > | >> |
| > | >> |
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|

Dec 16 '05 #12
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

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:FP**************@TK2MSFTNGXA02.phx.gbl...
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.HorizontalResolutio n,
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
<Mh**************@TK2MSFTNGXA02.phx.gbl>
<uf**************@TK2MSFTNGP10.phx.gbl>
| 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: <ei**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
| "David Lozzi" <Da********@nospam.nospam> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
| I updated the script a bit and also included your last post. Here's my
current script which is still receiving the GDI+ genereic error. I removed
the Try statement this code was in and it appears to be erroring on the
line with the *
| Dim img As Image
| img = Image.FromFile(p)
| Dim photo As New Bitmap(img)
| Dim photo2 As New Bitmap(img.HorizontalResolution,
img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)
| Dim grPhoto As Graphics
| grPhoto = Graphics.FromImage(photo)
| grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))
| grPhoto.Dispose()
| photo.Dispose()
| photo2.SetResolution(nw, nh)
| photo2.Save(p) *
| photo2.Dispose()
| Thanks!!
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:Mh**************@TK2MSFTNGXA02.phx.gbl...
| > Thanks for your response David,
| >
| > I think the NullReference exception is due to the graphic object is
not
| > correctly assigned. From the code you pasted:
| >
| > =================
| > Dim graphic As Graphics
| > graphic.FromImage(photo)
| > Dim img2 As Image
| > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| >
| > graphic.Dispose()
| > photo.Save(p)
| > =================
| >
| > You use graphic.FromImage(photo), this will return an Graphics
object
| > instance, and you need to assign it to the Graphics reference, like:
| >
| >
| > ........
| > Dim graphic As Graphics
| > graphic = Graphics.FromImage(photo)
| > ............
| >
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers
no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "David Lozzi" <Da********@nospam.nospam>
| > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > <#G**************@TK2MSFTNGP09.phx.gbl>
| > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | Subject: Re: Upload image and then resize it - A generic error
occurred
| > in GDI+.
| > | Date: Mon, 12 Dec 2005 09:40:02 -0500
| > | Lines: 229
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Response
| > | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:364219
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | The object reference error is occuring on the DrawImage line, if
that
| > helps.
| > |
| > | --
| > | David Lozzi
| > | Web Applications Developer
| > | dlozzi@(remove-this)delphi-ts.com
| > |
| > |
| > |
| > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | news:O3**************@TK2MSFTNGP15.phx.gbl...
| > | > After reviewing your code, this function returns the resized
image as
| > an
| > | > image to the calling script. I need the resized image to be
saved
to
| > disk.
| > | > My script is below. I get an error: Object reference not set to
an
| > | > instance of an object, which pertains to the graphic object
because
| > when I
| > | > remove that chunk of code, I get the same GDI+ error.
| > | >
| > | > Thanks!!
| > | >
| > | > Function ResizeImage(ByVal p As String, ByVal w As Integer,
ByVal h
| > As
| > | > Integer) As String
| > | > Dim img As System.Drawing.Image
| > | > img = System.Drawing.Image.FromFile(p)
| > | > Dim iw As Integer = img.Width
| > | > Dim ih As Integer = img.Height
| > | > Dim nw, nh As Integer
| > | > Dim per As Decimal
| > | >
| > | > If iw > w Or ih > h Then 'check to see if resize is
necessary
| > | > If iw > ih Then 'get the larger dimension and get
percentage
| > | > per = w / iw
| > | > Else
| > | > per = h / ih
| > | > End If
| > | >
| > | > 'create new sizes based on percentages
| > | > nw = iw * per
| > | > nh = ih * per
| > | >
| > | > 'now save it
| > | > Dim photo As New Bitmap(nw, nh,
| > | > Imaging.PixelFormat.Format24bppRgb)
| > | > photo.SetResolution(img.HorizontalResolution,
| > | > img.VerticalResolution)
| > | > Dim graphic As Graphics
| > | > graphic.FromImage(photo)
| > | > Dim img2 As Image
| > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh),
New
| > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | >
| > | > graphic.Dispose()
| > | > photo.Save(p)
| > | >
| > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw &
"x" & ih
| > &
| > | > "<br>New " & nw & "x" & nh
| > | > Else
| > | > lblTemp.Text = "No resize necessary."
| > | > End If
| > | > End Function
| > | >
| > | > --
| > | > David Lozzi
| > | > Web Applications Developer
| > | > dlozzi@(remove-this)delphi-ts.com
| > | >
| > | >
| > | >
| > | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
message
| > | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| > | >> Hey David,
| > | >>
| > | >> So this seems a pure GDI+ image processing issue. Does this
problem
| > | >> occurs
| > | >> only when the uploaded image is of certain format or is a
common
issue
| > | >> that
| > | >> will occur for any images that uploaded onto the server?
Generally I
| > | >> think
| > | >> we can throubleshoot through the following steps:
| > | >>
| > | >> 1. Make sure that the image is uploaded correctly onto the
server ,
| > open
| > | >> it
| > | >> in image viewer to make sure the data is not corrupted.
| > | >>
| > | >> 2. Then, use some standard image resizeing code to resize the
image...
| > | >> here is some simple GDI+ code I picked from net which resize
the
image
| > | >> through percentage value:
| > | >>
| > | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| > | >> {
| > | >> float nPercent = ((float)Percent/100);
| > | >>
| > | >> int sourceWidth = imgPhoto.Width;
| > | >> int sourceHeight = imgPhoto.Height;
| > | >> int sourceX = 0;
| > | >> int sourceY = 0;
| > | >>
| > | >> int destX = 0;
| > | >> int destY = 0;
| > | >> int destWidth = (int)(sourceWidth * nPercent);
| > | >> int destHeight = (int)(sourceHeight * nPercent);
| > | >>
| > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| > | >> PixelFormat.Format24bppRgb);
| > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| > | >> 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" <Da********@nospam.nospam>
| > | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | >> | Subject: Re: Upload image and then resize it - A generic
error
| > occurred
| > | >> in GDI+.
| > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| > | >> | Lines: 270
| > | >> | MIME-Version: 1.0
| > | >> | Content-Type: multipart/alternative;
| > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| > | >> | X-Priority: 3
| > | >> | X-MSMail-Priority: Normal
| > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
24.63.42.200
| > | >> | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | >> | Xref: TK2MSFTNGXA02.phx.gbl
| > | >> microsoft.public.dotnet.framework.aspnet:363864
| > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | >> |
| > | >> | Besides the point, I just realized my math is wrong, please
don't
| > | >> critique. I just need to resolve the resize issue.
| > | >> | --
| > | >> | David Lozzi
| > | >> | Web Applications Developer
| > | >> | dlozzi@(remove-this)delphi-ts.com
| > | >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | >> | Howdy,
| > | >> | I have a function that uploads an image and that works
great. I
| > love
| > | >> Nets built in upload, so much easier than 3rd party uploaders!
| > | >> | Now I am making a public function that will take the path
of
the
| >| >> uploaded image, and resize it with the provided dimensions. My
| > function
| > | >> is
| > | >> below. The current function is returning an error when run from
the
| > | >> upload
| > | >> function: A generic error occurred in GDI+. Not sure what
exactly that
| > | >> means. From what I can tell, no one really knows what it
means....
| > Here's
| > | >> my public function
| > | >> | Function ResizeImage(ByVal p As String, ByVal w As
Integer,
| > ByVal
| > | >> h
| > | >> As Integer) As String
| > | >> | Dim img As System.Drawing.Image
| > | >> | img = System.Drawing.Image.FromFile(p)
| > | >> | Dim iw As Integer = img.Width
| > | >> | Dim ih As Integer = img.Height
| > | >> | Dim nw, nh As Integer
| > | >> | Dim per As Decimal
| > | >> | If iw > w Or ih > h Then 'check to see if resize is
necessary

| > | >> | If w > h Then 'get the larger dimension and get
| >> percentage

| > | >> | per = iw / w
| > | >> | Else
| > | >> | per = ih / h
| > | >> | End If
| > | >> | 'create new sizes based on percentages
| > | >> | nw = iw * per
| > | >> | nh = ih * per
| > | >> | 'now save it
| > | >> | Dim size As New Size(nw, nh)
| > | >> | Dim nimg As New Bitmap(img, size)
| > | >> | nimg.Save(p)
| > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih &
"<br>New "
| > &
| > | >> nw
| > | >> & "x" & nh
| > | >> | Else
| > | >> | lblTemp.Text = "No resize necessary."
| > | >> | End If
| > | >> | End Function
| > | >> | and here is my code for the upload, abbreviated
| > | >> | If picType = "image/jpeg" Or picType =
"image/gif"
| > Or
| > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| > | >> | File.PostedFile.SaveAs(path)
| > | >> | ResizeImage(path, 120, 95)
| > | >> | lblError.Text = "The speaker photo
uploaded
| > | >> sucessfully! Make sure to click Update below to save changes."
| > | >> | Else
| > | >> | lblError.Text = "Invalid image format.
Only
| > JPG,
| > | >> JPEG, GIF and BMP images are allowed."
| > | >> | End If
| > | >> | Again, the upload works great
| > | >> | Thanks!!
| > | >> | --
| > | >> | David Lozzi
| > | >> | Web Applications Developer
| > | >> | dlozzi@(remove-this)delphi-ts.com
| > | >> |
| > | >> |
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|

Dec 16 '05 #13
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
<Mh**************@TK2MSFTNGXA02.phx.gbl>
<uf**************@TK2MSFTNGP10.phx.gbl>
<ei**************@TK2MSFTNGP11.phx.gbl>
<FP**************@TK2MSFTNGXA02.phx.gbl>
| 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: <#B*************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.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
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:FP**************@TK2MSFTNGXA02.phx.gbl...
| > 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.HorizontalResolutio n,
| > 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" <Da********@nospam.nospam>
| > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > <#G**************@TK2MSFTNGP09.phx.gbl>
| > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > <O3**************@TK2MSFTNGP15.phx.gbl>
| > <O$**************@TK2MSFTNGP15.phx.gbl>
| > <Mh**************@TK2MSFTNGXA02.phx.gbl>
| > <uf**************@TK2MSFTNGP10.phx.gbl>
| > | 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: <ei**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
| > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > news:uf**************@TK2MSFTNGP10.phx.gbl...
| > | I updated the script a bit and also included your last post. Here's
my
| > current script which is still receiving the GDI+ genereic error. I
removed
| > the Try statement this code was in and it appears to be erroring on the
| > line with the *
| > | Dim img As Image
| > | img = Image.FromFile(p)
| > | Dim photo As New Bitmap(img)
| > | Dim photo2 As New Bitmap(img.HorizontalResolution,
| > img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)
| > | Dim grPhoto As Graphics
| > | grPhoto = Graphics.FromImage(photo)
| > | grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))
| > | grPhoto.Dispose()
| > | photo.Dispose()
| > | photo2.SetResolution(nw, nh)
| > | photo2.Save(p) *
| > | photo2.Dispose()
| > | Thanks!!
| > | --
| > | David Lozzi
| > | Web Applications Developer
| > | dlozzi@(remove-this)delphi-ts.com
| > |
| > | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > news:Mh**************@TK2MSFTNGXA02.phx.gbl...
| > | > Thanks for your response David,
| > | >
| > | > I think the NullReference exception is due to the graphic object
is
| > not
| > | > correctly assigned. From the code you pasted:
| > | >
| > | > =================
| > | > Dim graphic As Graphics
| > | > graphic.FromImage(photo)
| > | > Dim img2 As Image
| > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh), New
| > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | >
| > | > graphic.Dispose()
| > | > photo.Save(p)
| > | > =================
| > | >
| > | > You use graphic.FromImage(photo), this will return an Graphics
| > object
| > | > instance, and you need to assign it to the Graphics reference,
like:
| > | >
| > | >
| > | > ........
| > | > Dim graphic As Graphics
| > | > graphic = Graphics.FromImage(photo)
| > | > ............
| > | >
| > | >
| > | > Thanks,
| > | >
| > | > Steven Cheng
| > | > Microsoft Online Support
| > | >
| > | > Get Secure! www.microsoft.com/security
| > | > (This posting is provided "AS IS", with no warranties, and
confers
| > no
| > | > rights.)
| > | >
| > | >
| > | >
| > | >
| > | > --------------------
| > | > | From: "David Lozzi" <Da********@nospam.nospam>
| > | > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > | > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | > | Subject: Re: Upload image and then resize it - A generic error
| > occurred
| > | > in GDI+.
| > | > | Date: Mon, 12 Dec 2005 09:40:02 -0500
| > | > | Lines: 229
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | > | X-RFC2646: Format=Flowed; Response
| > | > | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
24.63.42.200
| > | > | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| > | > | Xref: TK2MSFTNGXA02.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet:364219
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > |
| > | > | The object reference error is occuring on the DrawImage line, if
| > that
| > | > helps.
| > | > |
| > | > | --
| > | > | David Lozzi
| > | > | Web Applications Developer
| > | > | dlozzi@(remove-this)delphi-ts.com
| > | > |
| > | > |
| > | > |
| > | > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | > | news:O3**************@TK2MSFTNGP15.phx.gbl...
| > | > | > After reviewing your code, this function returns the resized
| > image as
| > | > an
| > | > | > image to the calling script. I need the resized image to be
| > saved
| > to
| > | > disk.
| > | > | > My script is below. I get an error: Object reference not set
to
| > an
| > | > | > instance of an object, which pertains to the graphic object
| > because
| > | > when I
| > | > | > remove that chunk of code, I get the same GDI+ error.
| > | > | >
| > | > | > Thanks!!
| > | > | >
| > | > | > Function ResizeImage(ByVal p As String, ByVal w As Integer,
| > ByVal h
| > | > As
| > | > | > Integer) As String
| > | > | > Dim img As System.Drawing.Image
| > | > | > img = System.Drawing.Image.FromFile(p)
| > | > | > Dim iw As Integer = img.Width
| > | > | > Dim ih As Integer = img.Height
| > | > | > Dim nw, nh As Integer
| > | > | > Dim per As Decimal
| > | > | >
| > | > | > If iw > w Or ih > h Then 'check to see if resize is
| > necessary
| > | > | > If iw > ih Then 'get the larger dimension and get
| > percentage
| > | > | > per = w / iw
| > | > | > Else
| > | > | > per = h / ih
| > | > | > End If
| > | > | >
| > | > | > 'create new sizes based on percentages
| > | > | > nw = iw * per
| > | > | > nh = ih * per
| > | > | >
| > | > | > 'now save it
| > | > | > Dim photo As New Bitmap(nw, nh,
| > | > | > Imaging.PixelFormat.Format24bppRgb)
| > | > | > photo.SetResolution(img.HorizontalResolution,
| > | > | > img.VerticalResolution)
| > | > | > Dim graphic As Graphics
| > | > | > graphic.FromImage(photo)
| > | > | > Dim img2 As Image
| > | > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh),
| > New
| > | > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | > | >
| > | > | > graphic.Dispose()
| > | > | > photo.Save(p)
| > | > | >
| > | > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw &
| > "x" & ih
| > | > &
| > | > | > "<br>New " & nw & "x" & nh
| > | > | > Else
| > | > | > lblTemp.Text = "No resize necessary."
| > | > | > End If
| > | > | > End Function
| > | > | >
| > | > | > --
| > | > | > David Lozzi
| > | > | > Web Applications Developer
| > | > | > dlozzi@(remove-this)delphi-ts.com
| > | > | >
| > | > | >
| > | > | >
| > | > | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
| > message
| > | > | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| > | > | >> Hey David,
| > | > | >>
| > | > | >> So this seems a pure GDI+ image processing issue. Does this
| > problem
| > | > | >> occurs
| > | > | >> only when the uploaded image is of certain format or is a
| > common
| > issue
| > | > | >> that
| > | > | >> will occur for any images that uploaded onto the server?
| > Generally I
| > | > | >> think
| > | > | >> we can throubleshoot through the following steps:
| > | > | >>
| > | > | >> 1. Make sure that the image is uploaded correctly onto the
| > server ,
| > | > open
| > | > | >> it
| > | > | >> in image viewer to make sure the data is not corrupted.
| > | > | >>
| > | > | >> 2. Then, use some standard image resizeing code to resize the
| > image...
| > | > | >> here is some simple GDI+ code I picked from net which resize
| > the
| > image
| > | > | >> through percentage value:
| > | > | >>
| > | > | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| > | > | >> {
| > | > | >> float nPercent = ((float)Percent/100);
| > | > | >>
| > | > | >> int sourceWidth = imgPhoto.Width;
| > | > | >> int sourceHeight = imgPhoto.Height;
| > | > | >> int sourceX = 0;
| > | > | >> int sourceY = 0;
| > | > | >>
| > | > | >> int destX = 0;
| > | > | >> int destY = 0;
| > | > | >> int destWidth = (int)(sourceWidth * nPercent);
| > | > | >> int destHeight = (int)(sourceHeight * nPercent);
| > | > | >>
| > | > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| > | > | >> PixelFormat.Format24bppRgb);
| > | > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| > | > | >> 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" <Da********@nospam.nospam>
| > | > | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > | >> | Subject: Re: Upload image and then resize it - A generic
| > error
| > | > occurred
| > | > | >> in GDI+.
| > | > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| > | > | >> | Lines: 270
| > | > | >> | MIME-Version: 1.0
| > | > | >> | Content-Type: multipart/alternative;
| > | > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| > | > | >> | X-Priority: 3
| > | > | >> | X-MSMail-Priority: Normal
| > | > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | > | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
| > 24.63.42.200
| > | > | >> | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | > | >> | Xref: TK2MSFTNGXA02.phx.gbl
| > | > | >> microsoft.public.dotnet.framework.aspnet:363864
| > | > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > | >> |
| > | > | >> | Besides the point, I just realized my math is wrong, please
| > don't
| > | > | >> critique. I just need to resolve the resize issue.
| > | > | >> | --
| > | > | >> | David Lozzi
| > | > | >> | Web Applications Developer
| > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | >> | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | > | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | > | >> | Howdy,
| > | > | >> | I have a function that uploads an image and that works
| > great. I
| > | > love
| > | > | >> Nets built in upload, so much easier than 3rd party
uploaders!
| > | > | >> | Now I am making a public function that will take the
path
| > of
| > the
| > | >| >> uploaded image, and resize it with the provided dimensions. My
| > | > function
| > | > | >> is
| > | > | >> below. The current function is returning an error when run
from
| > the
| > | > | >> upload
| > | > | >> function: A generic error occurred in GDI+. Not sure what
| > exactly that
| > | > | >> means. From what I can tell, no one really knows what it
| > means....
| > | > Here's
| > | > | >> my public function
| > | > | >> | Function ResizeImage(ByVal p As String, ByVal w As
| > Integer,
| > | > ByVal
| > | > | >> h
| > | > | >> As Integer) As String
| > | > | >> | Dim img As System.Drawing.Image
| > | > | >> | img = System.Drawing.Image.FromFile(p)
| > | > | >> | Dim iw As Integer = img.Width
| > | > | >> | Dim ih As Integer = img.Height
| > | > | >> | Dim nw, nh As Integer
| > | > | >> | Dim per As Decimal
| > | > | >> | If iw > w Or ih > h Then 'check to see if resize
is
| > > necessary
| > | > | >> | If w > h Then 'get the larger dimension and
get
| > > | >> percentage
| > | > | >> | per = iw / w
| > | > | >> | Else
| > | > | >> | per = ih / h
| > | > | >> | End If
| > | > | >> | 'create new sizes based on percentages
| > | > | >> | nw = iw * per
| > | > | >> | nh = ih * per
| > | > | >> | 'now save it
| > | > | >> | Dim size As New Size(nw, nh)
| > | > | >> | Dim nimg As New Bitmap(img, size)
| > | > | >> | nimg.Save(p)
| > | > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih &
| > "<br>New "
| > | > &
| > | > | >> nw
| > | > | >> & "x" & nh
| > | > | >> | Else
| > | > | >> | lblTemp.Text = "No resize necessary."
| > | > | >> | End If
| > | > | >> | End Function
| > | > | >> | and here is my code for the upload, abbreviated
| > | > | >> | If picType = "image/jpeg" Or picType =
| > "image/gif"
| > | > Or
| > | > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| > | > | >> | File.PostedFile.SaveAs(path)
| > | > | >> | ResizeImage(path, 120, 95)
| > | > | >> | lblError.Text = "The speaker photo
| > uploaded
| > | > | >> sucessfully! Make sure to click Update below to save
changes."
| > | > | >> | Else
| > | > | >> | lblError.Text = "Invalid image
format.
| > Only
| > | > JPG,
| > | > | >> JPEG, GIF and BMP images are allowed."
| > | > | >> | End If
| > | > | >> | Again, the upload works great
| > | > | >> | Thanks!!
| > | > | >> | --
| > | > | >> | David Lozzi
| > | > | >> | Web Applications Developer
| > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | >> |
| > | > | >> |
| > | > | >>
| > | > | >
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| >
|
|
|

Dec 19 '05 #14
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

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:e3**************@TK2MSFTNGXA02.phx.gbl...
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
<Mh**************@TK2MSFTNGXA02.phx.gbl>
<uf**************@TK2MSFTNGP10.phx.gbl>
<ei**************@TK2MSFTNGP11.phx.gbl>
<FP**************@TK2MSFTNGXA02.phx.gbl>
| 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: <#B*************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.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
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:FP**************@TK2MSFTNGXA02.phx.gbl...
| > 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.HorizontalResolutio n,
| > 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" <Da********@nospam.nospam>
| > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > <#G**************@TK2MSFTNGP09.phx.gbl>
| > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > <O3**************@TK2MSFTNGP15.phx.gbl>
| > <O$**************@TK2MSFTNGP15.phx.gbl>
| > <Mh**************@TK2MSFTNGXA02.phx.gbl>
| > <uf**************@TK2MSFTNGP10.phx.gbl>
| > | 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: <ei**************@TK2MSFTNGP11.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
| > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > news:uf**************@TK2MSFTNGP10.phx.gbl...
| > | I updated the script a bit and also included your last post.
Here's
my
| > current script which is still receiving the GDI+ genereic error. I
removed
| > the Try statement this code was in and it appears to be erroring on
the
| > line with the *
| > | Dim img As Image
| > | img = Image.FromFile(p)
| > | Dim photo As New Bitmap(img)
| > | Dim photo2 As New Bitmap(img.HorizontalResolution,
| > img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)
| > | Dim grPhoto As Graphics
| > | grPhoto = Graphics.FromImage(photo)
| > | grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))
| > | grPhoto.Dispose()
| > | photo.Dispose()
| > | photo2.SetResolution(nw, nh)
| > | photo2.Save(p) *
| > | photo2.Dispose()
| > | Thanks!!
| > | --
| > | David Lozzi
| > | Web Applications Developer
| > | dlozzi@(remove-this)delphi-ts.com
| > |
| > | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
message
| > news:Mh**************@TK2MSFTNGXA02.phx.gbl...
| > | > Thanks for your response David,
| > | >
| > | > I think the NullReference exception is due to the graphic object
is
| > not
| > | > correctly assigned. From the code you pasted:
| > | >
| > | > =================
| > | > Dim graphic As Graphics
| > | > graphic.FromImage(photo)
| > | > Dim img2 As Image
| > | > graphic.DrawImage(img, New Rectangle(0, 0, nw, nh),
New
| > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | >
| > | > graphic.Dispose()
| > | > photo.Save(p)
| > | > =================
| > | >
| > | > You use graphic.FromImage(photo), this will return an Graphics
| > object
| > | > instance, and you need to assign it to the Graphics reference,
like:
| > | >
| > | >
| > | > ........
| > | > Dim graphic As Graphics
| > | > graphic = Graphics.FromImage(photo)
| > | > ............
| > | >
| > | >
| > | > Thanks,
| > | >
| > | > Steven Cheng
| > | > Microsoft Online Support
| > | >
| > | > Get Secure! www.microsoft.com/security
| > | > (This posting is provided "AS IS", with no warranties, and
confers
| > no
| > | > rights.)
| > | >
| > | >
| > | >
| > | >
| > | > --------------------
| > | > | From: "David Lozzi" <Da********@nospam.nospam>
| > | > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > | > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | > | Subject: Re: Upload image and then resize it - A generic error
| > occurred
| > | > in GDI+.
| > | > | Date: Mon, 12 Dec 2005 09:40:02 -0500
| > | > | Lines: 229
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | > | X-RFC2646: Format=Flowed; Response
| > | > | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
24.63.42.200
| > | > | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| > | > | Xref: TK2MSFTNGXA02.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet:364219
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > |
| > | > | The object reference error is occuring on the DrawImage line,
if
| > that
| > | > helps.
| > | > |
| > | > | --
| > | > | David Lozzi
| > | > | Web Applications Developer
| > | > | dlozzi@(remove-this)delphi-ts.com
| > | > |
| > | > |
| > | > |
| > | > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | > | news:O3**************@TK2MSFTNGP15.phx.gbl...
| > | > | > After reviewing your code, this function returns the resized
| > image as
| > | > an
| > | > | > image to the calling script. I need the resized image to be
| > saved
| > to
| > | > disk.
| > | > | > My script is below. I get an error: Object reference not set
to
| > an
| > | > | > instance of an object, which pertains to the graphic object
| > because
| > | > when I
| > | > | > remove that chunk of code, I get the same GDI+ error.
| > | > | >
| > | > | > Thanks!!
| > | > | >
| > | > | > Function ResizeImage(ByVal p As String, ByVal w As
Integer,
| > ByVal h
| > | > As
| > | > | > Integer) As String
| > | > | > Dim img As System.Drawing.Image
| > | > | > img = System.Drawing.Image.FromFile(p)
| > | > | > Dim iw As Integer = img.Width
| > | > | > Dim ih As Integer = img.Height
| > | > | > Dim nw, nh As Integer
| > | > | > Dim per As Decimal
| > | > | >
| > | > | > If iw > w Or ih > h Then 'check to see if resize is
| > necessary
| > | > | > If iw > ih Then 'get the larger dimension and get
| > percentage
| > | > | > per = w / iw
| > | > | > Else
| > | > | > per = h / ih
| > | > | > End If
| > | > | >
| > | > | > 'create new sizes based on percentages
| > | > | > nw = iw * per
| > | > | > nh = ih * per
| > | > | >
| > | > | > 'now save it
| > | > | > Dim photo As New Bitmap(nw, nh,
| > | > | > Imaging.PixelFormat.Format24bppRgb)
| > | > | > photo.SetResolution(img.HorizontalResolution,
| > | > | > img.VerticalResolution)
| > | > | > Dim graphic As Graphics
| > | > | > graphic.FromImage(photo)
| > | > | > Dim img2 As Image
| > | > | > graphic.DrawImage(img, New Rectangle(0, 0, nw,
nh),
| > New
| > | > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | > | >
| > | > | > graphic.Dispose()
| > | > | > photo.Save(p)
| > | > | >
| > | > | > lblTemp.Text = "<br>Per " & per & "<br>Old " & iw
&
| > "x" & ih
| > | > &
| > | > | > "<br>New " & nw & "x" & nh
| > | > | > Else
| > | > | > lblTemp.Text = "No resize necessary."
| > | > | > End If
| > | > | > End Function
| > | > | >
| > | > | > --
| > | > | > David Lozzi
| > | > | > Web Applications Developer
| > | > | > dlozzi@(remove-this)delphi-ts.com
| > | > | >
| > | > | >
| > | > | >
| > | > | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
| > message
| > | > | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| > | > | >> Hey David,
| > | > | >>
| > | > | >> So this seems a pure GDI+ image processing issue. Does this
| > problem
| > | > | >> occurs
| > | > | >> only when the uploaded image is of certain format or is a
| > common
| > issue
| > | > | >> that
| > | > | >> will occur for any images that uploaded onto the server?
| > Generally I
| > | > | >> think
| > | > | >> we can throubleshoot through the following steps:
| > | > | >>
| > | > | >> 1. Make sure that the image is uploaded correctly onto the
| > server ,
| > | > open
| > | > | >> it
| > | > | >> in image viewer to make sure the data is not corrupted.
| > | > | >>
| > | > | >> 2. Then, use some standard image resizeing code to resize
the
| > image...
| > | > | >> here is some simple GDI+ code I picked from net which
resize
| > the
| > image
| > | > | >> through percentage value:
| > | > | >>
| > | > | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| > | > | >> {
| > | > | >> float nPercent = ((float)Percent/100);
| > | > | >>
| > | > | >> int sourceWidth = imgPhoto.Width;
| > | > | >> int sourceHeight = imgPhoto.Height;
| > | > | >> int sourceX = 0;
| > | > | >> int sourceY = 0;
| > | > | >>
| > | > | >> int destX = 0;
| > | > | >> int destY = 0;
| > | > | >> int destWidth = (int)(sourceWidth * nPercent);
| > | > | >> int destHeight = (int)(sourceHeight * nPercent);
| > | > | >>
| > | > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| > | > | >> PixelFormat.Format24bppRgb);
| > | > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| > | > | >> 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" <Da********@nospam.nospam>
| > | > | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > | >> | Subject: Re: Upload image and then resize it - A generic
| > error
| > | > occurred
| > | > | >> in GDI+.
| > | > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| > | > | >> | Lines: 270
| > | > | >> | MIME-Version: 1.0
| > | > | >> | Content-Type: multipart/alternative;
| > | > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| > | > | >> | X-Priority: 3
| > | > | >> | X-MSMail-Priority: Normal
| > | > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | > | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
| > 24.63.42.200
| > | > | >> | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | > | >> | Xref: TK2MSFTNGXA02.phx.gbl
| > | > | >> microsoft.public.dotnet.framework.aspnet:363864
| > | > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > | >> |
| > | > | >> | Besides the point, I just realized my math is wrong,
please
| > don't
| > | > | >> critique. I just need to resolve the resize issue.
| > | > | >> | --
| > | > | >> | David Lozzi
| > | > | >> | Web Applications Developer
| > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | >> | "David Lozzi" <Da********@nospam.nospam> wrote in
message
| > | > | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | > | >> | Howdy,
| > | > | >> | I have a function that uploads an image and that works
| > great. I
| > | > love
| > | > | >> Nets built in upload, so much easier than 3rd party
uploaders!
| > | > | >> | Now I am making a public function that will take the
path
| > of
| > the
| > | >| >> uploaded image, and resize it with the provided dimensions.
My
| > | > function
| > | > | >> is
| > | > | >> below. The current function is returning an error when run
from
| > the
| > | > | >> upload
| > | > | >> function: A generic error occurred in GDI+. Not sure what
| > exactly that
| > | > | >> means. From what I can tell, no one really knows what it
| > means....
| > | > Here's
| > | > | >> my public function
| > | > | >> | Function ResizeImage(ByVal p As String, ByVal w As
| > Integer,
| > | > ByVal
| > | > | >> h
| > | > | >> As Integer) As String
| > | > | >> | Dim img As System.Drawing.Image
| > | > | >> | img = System.Drawing.Image.FromFile(p)
| > | > | >> | Dim iw As Integer = img.Width
| > | > | >> | Dim ih As Integer = img.Height
| > | > | >> | Dim nw, nh As Integer
| > | > | >> | Dim per As Decimal
| > | > | >> | If iw > w Or ih > h Then 'check to see if
resize
is
| > > necessary
| > | > | >> | If w > h Then 'get the larger dimension and
get
| > > | >> percentage
| > | > | >> | per = iw / w
| > | > | >> | Else
| > | > | >> | per = ih / h
| > | > | >> | End If
| > | > | >> | 'create new sizes based on percentages
| > | > | >> | nw = iw * per
| > | > | >> | nh = ih * per
| > | > | >> | 'now save it
| > | > | >> | Dim size As New Size(nw, nh)
| > | > | >> | Dim nimg As New Bitmap(img, size)
| > | > | >> | nimg.Save(p)
| > | > | >> | lblTemp.Text = "<br>Old " & iw & "x" & ih &
| > "<br>New "
| > | > &
| > | > | >> nw
| > | > | >> & "x" & nh
| > | > | >> | Else
| > | > | >> | lblTemp.Text = "No resize necessary."
| > | > | >> | End If
| > | > | >> | End Function
| > | > | >> | and here is my code for the upload, abbreviated
| > | > | >> | If picType = "image/jpeg" Or picType =
| > "image/gif"
| > | > Or
| > | > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| > | > | >> | File.PostedFile.SaveAs(path)
| > | > | >> | ResizeImage(path, 120, 95)
| > | > | >> | lblError.Text = "The speaker photo
| > uploaded
| > | > | >> sucessfully! Make sure to click Update below to save
changes."
| > | > | >> | Else
| > | > | >> | lblError.Text = "Invalid image
format.
| > Only
| > | > JPG,
| > | > | >> JPEG, GIF and BMP images are allowed."
| > | > | >> | End If
| > | > | >> | Again, the upload works great
| > | > | >> | Thanks!!
| > | > | >> | --
| > | > | >> | David Lozzi
| > | > | >> | Web Applications Developer
| > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | >> |
| > | > | >> |
| > | > | >>
| > | > | >
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| >
|
|
|

Dec 19 '05 #15
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" <Da********@nospam.nospam>
| References: <#r**************@TK2MSFTNGP09.phx.gbl>
<#G**************@TK2MSFTNGP09.phx.gbl>
<v1**************@TK2MSFTNGXA02.phx.gbl>
<O3**************@TK2MSFTNGP15.phx.gbl>
<O$**************@TK2MSFTNGP15.phx.gbl>
<Mh**************@TK2MSFTNGXA02.phx.gbl>
<uf**************@TK2MSFTNGP10.phx.gbl>
<ei**************@TK2MSFTNGP11.phx.gbl>
<FP**************@TK2MSFTNGXA02.phx.gbl>
<#B*************@TK2MSFTNGP12.phx.gbl>
<e3**************@TK2MSFTNGXA02.phx.gbl>
| 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: <#B*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:e3**************@TK2MSFTNGXA02.phx.gbl...
| > 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" <Da********@nospam.nospam>
| > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > <#G**************@TK2MSFTNGP09.phx.gbl>
| > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > <O3**************@TK2MSFTNGP15.phx.gbl>
| > <O$**************@TK2MSFTNGP15.phx.gbl>
| > <Mh**************@TK2MSFTNGXA02.phx.gbl>
| > <uf**************@TK2MSFTNGP10.phx.gbl>
| > <ei**************@TK2MSFTNGP11.phx.gbl>
| > <FP**************@TK2MSFTNGXA02.phx.gbl>
| > | 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: <#B*************@TK2MSFTNGP12.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.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
| > |
| > |
| > |
| > | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > | news:FP**************@TK2MSFTNGXA02.phx.gbl...
| > | > 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.HorizontalResolutio n,
| > | > 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" <Da********@nospam.nospam>
| > | > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > | > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | > <O$**************@TK2MSFTNGP15.phx.gbl>
| > | > <Mh**************@TK2MSFTNGXA02.phx.gbl>
| > | > <uf**************@TK2MSFTNGP10.phx.gbl>
| > | > | 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: <ei**************@TK2MSFTNGP11.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200
| > | > | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.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
| > | > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | > news:uf**************@TK2MSFTNGP10.phx.gbl...
| > | > | I updated the script a bit and also included your last post.
| > Here's
| > my
| > | > current script which is still receiving the GDI+ genereic error. I
| > removed
| > | > the Try statement this code was in and it appears to be erroring on
| > the
| > | > line with the *
| > | > | Dim img As Image
| > | > | img = Image.FromFile(p)
| > | > | Dim photo As New Bitmap(img)
| > | > | Dim photo2 As New Bitmap(img.HorizontalResolution,
| > | > img.VerticalResolution, Imaging.PixelFormat.Format32bppRgb)
| > | > | Dim grPhoto As Graphics
| > | > | grPhoto = Graphics.FromImage(photo)
| > | > | grPhoto.DrawImage(photo2, New Rectangle(0, 0, iw, ih))
| > | > | grPhoto.Dispose()
| > | > | photo.Dispose()
| > | > | photo2.SetResolution(nw, nh)
| > | > | photo2.Save(p) *
| > | > | photo2.Dispose()
| > | > | Thanks!!
| > | > | --
| > | > | David Lozzi
| > | > | Web Applications Developer
| > | > | dlozzi@(remove-this)delphi-ts.com
| > | > |
| > | > | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in
| > message
| > | > news:Mh**************@TK2MSFTNGXA02.phx.gbl...
| > | > | > Thanks for your response David,
| > | > | >
| > | > | > I think the NullReference exception is due to the graphic
object
| > is
| > | > not
| > | > | > correctly assigned. From the code you pasted:
| > | > | >
| > | > | > =================
| > | > | > Dim graphic As Graphics
| > | > | > graphic.FromImage(photo)
| > | > | > Dim img2 As Image
| > | > | > graphic.DrawImage(img, New Rectangle(0, 0, nw,
nh),
| > New
| > | > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | > | >
| > | > | > graphic.Dispose()
| > | > | > photo.Save(p)
| > | > | > =================
| > | > | >
| > | > | > You use graphic.FromImage(photo), this will return an Graphics
| > | > object
| > | > | > instance, and you need to assign it to the Graphics reference,
| > like:
| > | > | >
| > | > | >
| > | > | > ........
| > | > | > Dim graphic As Graphics
| > | > | > graphic = Graphics.FromImage(photo)
| > | > | > ............
| > | > | >
| > | > | >
| > | > | > Thanks,
| > | > | >
| > | > | > Steven Cheng
| > | > | > Microsoft Online Support
| > | > | >
| > | > | > Get Secure! www.microsoft.com/security
| > | > | > (This posting is provided "AS IS", with no warranties, and
| > confers
| > | > no
| > | > | > rights.)
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | > --------------------
| > | > | > | From: "David Lozzi" <Da********@nospam.nospam>
| > | > | > | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > | > <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > | > <v1**************@TK2MSFTNGXA02.phx.gbl>
| > | > | > <O3**************@TK2MSFTNGP15.phx.gbl>
| > | > | > | Subject: Re: Upload image and then resize it - A generic
error
| > | > occurred
| > | > | > in GDI+.
| > | > | > | Date: Mon, 12 Dec 2005 09:40:02 -0500
| > | > | > | Lines: 229
| > | > | > | X-Priority: 3
| > | > | > | X-MSMail-Priority: Normal
| > | > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | > | > | X-RFC2646: Format=Flowed; Response
| > | > | > | Message-ID: <O$**************@TK2MSFTNGP15.phx.gbl>
| > | > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | > | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
| > 24.63.42.200
| > | > | > | Path:
| > | > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
| > | > | > | Xref: TK2MSFTNGXA02.phx.gbl
| > | > | > microsoft.public.dotnet.framework.aspnet:364219
| > | > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > | > |
| > | > | > | The object reference error is occuring on the DrawImage
line,
| > if
| > | > that
| > | > | > helps.
| > | > | > |
| > | > | > | --
| > | > | > | David Lozzi
| > | > | > | Web Applications Developer
| > | > | > | dlozzi@(remove-this)delphi-ts.com
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | > | "David Lozzi" <Da********@nospam.nospam> wrote in message
| > | > | > | news:O3**************@TK2MSFTNGP15.phx.gbl...
| > | > | > | > After reviewing your code, this function returns the
resized
| > | > image as
| > | > | > an
| > | > | > | > image to the calling script. I need the resized image to
be
| > | > saved
| > | > to
| > | > | > disk.
| > | > | > | > My script is below. I get an error: Object reference not
set
| > to
| > | > an
| > | > | > | > instance of an object, which pertains to the graphic
object
| > | > because
| > | > | > when I
| > | > | > | > remove that chunk of code, I get the same GDI+ error.
| > | > | > | >
| > | > | > | > Thanks!!
| > | > | > | >
| > | > | > | > Function ResizeImage(ByVal p As String, ByVal w As
| > Integer,
| > | > ByVal h
| > | > | > As
| > | > | > | > Integer) As String
| > | > | > | > Dim img As System.Drawing.Image
| > | > | > | > img = System.Drawing.Image.FromFile(p)
| > | > | > | > Dim iw As Integer = img.Width
| > | > | > | > Dim ih As Integer = img.Height
| > | > | > | > Dim nw, nh As Integer
| > | > | > | > Dim per As Decimal
| > | > | > | >
| > | > | > | > If iw > w Or ih > h Then 'check to see if resize is
| > | > necessary
| > | > | > | > If iw > ih Then 'get the larger dimension and
get
| > | > percentage
| > | > | > | > per = w / iw
| > | > | > | > Else
| > | > | > | > per = h / ih
| > | > | > | > End If
| > | > | > | >
| > | > | > | > 'create new sizes based on percentages
| > | > | > | > nw = iw * per
| > | > | > | > nh = ih * per
| > | > | > | >
| > | > | > | > 'now save it
| > | > | > | > Dim photo As New Bitmap(nw, nh,
| > | > | > | > Imaging.PixelFormat.Format24bppRgb)
| > | > | > | > photo.SetResolution(img.HorizontalResolution,
| > | > | > | > img.VerticalResolution)
| > | > | > | > Dim graphic As Graphics
| > | > | > | > graphic.FromImage(photo)
| > | > | > | > Dim img2 As Image
| > | > | > | > graphic.DrawImage(img, New Rectangle(0, 0, nw,
| > nh),
| > | > New
| > | > | > | > Rectangle(0, 0, iw, ih), GraphicsUnit.Pixel)
| > | > | > | >
| > | > | > | > graphic.Dispose()
| > | > | > | > photo.Save(p)
| > | > | > | >
| > | > | > | > lblTemp.Text = "<br>Per " & per & "<br>Old " &
iw
| > &
| > | > "x" & ih
| > | > | > &
| > | > | > | > "<br>New " & nw & "x" & nh
| > | > | > | > Else
| > | > | > | > lblTemp.Text = "No resize necessary."
| > | > | > | > End If
| > | > | > | > End Function
| > | > | > | >
| > | > | > | > --
| > | > | > | > David Lozzi
| > | > | > | > Web Applications Developer
| > | > | > | > dlozzi@(remove-this)delphi-ts.com
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | > "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote
in
| > | > message
| > | > | > | > news:v1****************@TK2MSFTNGXA02.phx.gbl...
| > | > | > | >> Hey David,
| > | > | > | >>
| > | > | > | >> So this seems a pure GDI+ image processing issue. Does
this
| > | > problem
| > | > | > | >> occurs
| > | > | > | >> only when the uploaded image is of certain format or is a
| > | > common
| > | > issue
| > | > | > | >> that
| > | > | > | >> will occur for any images that uploaded onto the server?
| > | > Generally I
| > | > | > | >> think
| > | > | > | >> we can throubleshoot through the following steps:
| > | > | > | >>
| > | > | > | >> 1. Make sure that the image is uploaded correctly onto
the
| > | > server ,
| > | > | > open
| > | > | > | >> it
| > | > | > | >> in image viewer to make sure the data is not corrupted.
| > | > | > | >>
| > | > | > | >> 2. Then, use some standard image resizeing code to
resize
| > the
| > | > image...
| > | > | > | >> here is some simple GDI+ code I picked from net which
| > resize
| > | > the
| > | > image
| > | > | > | >> through percentage value:
| > | > | > | >>
| > | > | > | >> static Image ScaleByPercent(Image imgPhoto, int Percent)
| > | > | > | >> {
| > | > | > | >> float nPercent = ((float)Percent/100);
| > | > | > | >>
| > | > | > | >> int sourceWidth = imgPhoto.Width;
| > | > | > | >> int sourceHeight = imgPhoto.Height;
| > | > | > | >> int sourceX = 0;
| > | > | > | >> int sourceY = 0;
| > | > | > | >>
| > | > | > | >> int destX = 0;
| > | > | > | >> int destY = 0;
| > | > | > | >> int destWidth = (int)(sourceWidth * nPercent);
| > | > | > | >> int destHeight = (int)(sourceHeight * nPercent);
| > | > | > | >>
| > | > | > | >> Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
| > | > | > | >> PixelFormat.Format24bppRgb);
| > | > | > | >> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
| > | > | > | >> 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" <Da********@nospam.nospam>
| > | > | > | >> | References: <#r**************@TK2MSFTNGP09.phx.gbl>
| > | > | > | >> | Subject: Re: Upload image and then resize it - A
generic
| > | > error
| > | > | > occurred
| > | > | > | >> in GDI+.
| > | > | > | >> | Date: Fri, 9 Dec 2005 11:15:17 -0500
| > | > | > | >> | Lines: 270
| > | > | > | >> | MIME-Version: 1.0
| > | > | > | >> | Content-Type: multipart/alternative;
| > | > | > | >> | boundary="----=_NextPart_000_0011_01C5FCB1.D5B1AFD0"
| > | > | > | >> | X-Priority: 3
| > | > | > | >> | X-MSMail-Priority: Normal
| > | > | > | >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | > | > | >> | X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2900.2670
| > | > | > | >> | Message-ID: <#G**************@TK2MSFTNGP09.phx.gbl>
| > | > | > | >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | > | >> | NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.comcast.net
| > | > 24.63.42.200
| > | > | > | >> | Path:
| > | > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | > | > | >> | Xref: TK2MSFTNGXA02.phx.gbl
| > | > | > | >> microsoft.public.dotnet.framework.aspnet:363864
| > | > | > | >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > | > | >> |
| > | > | > | >> | Besides the point, I just realized my math is wrong,
| > please
| > | > don't
| > | > | > | >> critique. I just need to resolve the resize issue.
| > | > | > | >> | --
| > | > | > | >> | David Lozzi
| > | > | > | >> | Web Applications Developer
| > | > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | > | >> | "David Lozzi" <Da********@nospam.nospam> wrote in
| > message
| > | > | > | >> news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | > | > | >> | Howdy,
| > | > | > | >> | I have a function that uploads an image and that
works
| > | > great. I
| > | > | > love
| > | > | > | >> Nets built in upload, so much easier than 3rd party
| > uploaders!
| > | > | > | >> | Now I am making a public function that will take the
| > path
| > | > of
| > | > the
| > | > | >| >> uploaded image, and resize it with the provided
dimensions.
| > My
| > | > | > function
| > | > | > | >> is
| > | > | > | >> below. The current function is returning an error when
run
| > from
| > | > the
| > | > | > | >> upload
| > | > | > | >> function: A generic error occurred in GDI+. Not sure what
| > | > exactly that
| > | > | > | >> means. From what I can tell, no one really knows what it
| > | > means....
| > | > | > Here's
| > | > | > | >> my public function
| > | > | > | >> | Function ResizeImage(ByVal p As String, ByVal w
As
| > | > Integer,
| > | > | > ByVal
| > | > | > | >> h
| > | > | > | >> As Integer) As String
| > | > | > | >> | Dim img As System.Drawing.Image
| > | > | > | >> | img = System.Drawing.Image.FromFile(p)
| > | > | > | >> | Dim iw As Integer = img.Width
| > | > | > | >> | Dim ih As Integer = img.Height
| > | > | > | >> | Dim nw, nh As Integer
| > | > | > | >> | Dim per As Decimal
| > | > | > | >> | If iw > w Or ih > h Then 'check to see if
| > resize
| > is
| > | > > necessary
| > | > | > | >> | If w > h Then 'get the larger dimension
and
| > get
| > | > > | >> percentage
| > | > | > | >> | per = iw / w
| > | > | > | >> | Else
| > | > | > | >> | per = ih / h
| > | > | > | >> | End If
| > | > | > | >> | 'create new sizes based on percentages
| > | > | > | >> | nw = iw * per
| > | > | > | >> | nh = ih * per
| > | > | > | >> | 'now save it
| > | > | > | >> | Dim size As New Size(nw, nh)
| > | > | > | >> | Dim nimg As New Bitmap(img, size)
| > | > | > | >> | nimg.Save(p)
| > | > | > | >> | lblTemp.Text = "<br>Old " & iw & "x" &
ih &
| > | > "<br>New "
| > | > | > &
| > | > | > | >> nw
| > | > | > | >> & "x" & nh
| > | > | > | >> | Else
| > | > | > | >> | lblTemp.Text = "No resize necessary."
| > | > | > | >> | End If
| > | > | > | >> | End Function
| > | > | > | >> | and here is my code for the upload, abbreviated
| > | > | > | >> | If picType = "image/jpeg" Or picType
=
| > | > "image/gif"
| > | > | > Or
| > | > | > | >> picType = "image/pjpeg" Or picType = "image/bmp" Then
| > | > | > | >> | File.PostedFile.SaveAs(path)
| > | > | > | >> | ResizeImage(path, 120, 95)
| > | > | > | >> | lblError.Text = "The speaker
photo
| > | > uploaded
| > | > | > | >> sucessfully! Make sure to click Update below to save
| > changes."
| > | > | > | >> | Else
| > | > | > | >> | lblError.Text = "Invalid image
| > format.
| > | > Only
| > | > | > JPG,
| > | > | > | >> JPEG, GIF and BMP images are allowed."
| > | > | > | >> | End If
| > | > | > | >> | Again, the upload works great
| > | > | > | >> | Thanks!!
| > | > | > | >> | --
| > | > | > | >> | David Lozzi
| > | > | > | >> | Web Applications Developer
| > | > | > | >> | dlozzi@(remove-this)delphi-ts.com
| > | > | > | >> |
| > | > | > | >> |
| > | > | > | >>
| > | > | > | >
| > | > | > | >
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | >
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|

Dec 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alexli | last post by:
I got the error ================= An unhandled exception of typ 'System.Runtime.InteropServices.ExternalException' occurred i system.drawing.dl Additional information: A generic error occurred...
1
by: Prasad More | last post by:
Hello, I am trying to write a text on Multi-page TIFF image using C# and .NET GDI+. I have written following code to do this. When I execute this code I get "Invalid Parameter User. at...
2
by: Barry | last post by:
Hi Does anyone know why i am getting the following error message (line 64) , if i change to objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg) i do not get this error Exception...
3
by: Brent Burkart | last post by:
I have a shopping cart where I have a product page which has an image webcontrol. In my code behind, I load pictures into the image webcontrol. I would like to dynamically resize the picture...
1
by: Fahad Aijaz | last post by:
Hi, I am trying to display a Png image via ASP .NET. I get the image from the stream, creates the Bitmap, set the content type to Image/Png, but it give an error. If I set the content type to...
2
by: Adam J Knight | last post by:
Hi all, I am trying resize an image using the following code. //resize image System.Drawing.Image ImageUpload =...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
2
by: geertolaerts | last post by:
Hello, I have a problem with uploading off images to my webservice, i convert them to a byte at client side but something goes wrong when i try to convert them back to a image at server side....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.