上传图像然后调整大小 - GDI +中发生一般错误。 [英] Upload image and then resize it - A generic error occurred in GDI+.

查看:85
本文介绍了上传图像然后调整大小 - GDI +中发生一般错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个上传图像的功能,效果很好。我喜欢..网上内置,比第三方上传容易得多!


现在我正在创建一个公共功能,它将采用上传图像的路径,并调整大小提供的尺寸。我的功能如下。当从上传功能运行时,当前函数返回错误:GDI +中发生一般错误。不知道究竟是什么意思。据我所知,没有人真正知道这意味着什么......这是我的公共功能


函数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


如果iw>或者我是> h然后''检查是否需要调整大小

如果w> h然后''得到更大的尺寸并得到百分比

per = iw / w

否则

per = ih / h

结束如果


''根据百分比创建新尺寸

nw = iw * per

nh = ih *每个


''现在保存它

昏暗的大小为新的大小(nw,nh)

Dim nimg As New Bitmap (img,size)

nimg.Save(p)


lblTemp.Text ="< br> Old" &安培; iw&英寸×" &安培;呃& < br> New &安培; nw&英寸×" &安培; nh

Else

lblTemp.Text ="不需要调整大小。

结束如果

结束功能


这里是我的上传代码,缩写


如果picType =" image / jpeg"或者picType =" image / gif"或者picType =" image / pjpeg"或者picType =" image / bmp"然后

File.PostedFile.SaveAs(路径)

ResizeImage(路径,120,95)

lblError.Text ="扬声器照片上传成功!确保单击下面的更新以保存更改。

否则

lblError.Text ="无效的图像格式。只允许使用JPG,JPEG,GIF和BMP图像。

结束如果


再次,上传效果很好


谢谢!!


-

David Lozzi

Web应用程序开发人员
dlozzi @(删除 - 这个)delphi-ts.com

解决方案

除了这一点,我刚刚意识到我的数学错误,请不要批评。我只需要解决调整大小问题。


-

David Lozzi

Web应用程序开发人员
dlozzi @(remove-this)delphi-ts.com


" David Lozzi" <沓******** @ nospam.nospam>在留言新闻中写道:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

你好,


我有一个上传图像的功能,效果很好。我喜欢..网上内置,比第三方上传容易得多!


现在我正在创建一个公共功能,它将采用上传图像的路径,并调整大小提供的尺寸。我的功能如下。当从上传功能运行时,当前函数返回错误:GDI +中发生一般错误。不知道究竟是什么意思。据我所知,没有人真正知道这意味着什么......这是我的公共功能


函数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


如果iw>或者我是> h然后''检查是否需要调整大小

如果w> h然后''得到更大的尺寸并得到百分比

per = iw / w

否则

per = ih / h

结束如果


''根据百分比创建新尺寸

nw = iw * per

nh = ih *每个


''现在保存它

昏暗的大小为新的大小(nw,nh)

Dim nimg As New Bitmap (img,size)

nimg.Save(p)


lblTemp.Text ="< br> Old" &安培; iw&英寸×" &安培;呃& < br> New &安培; nw&英寸×" &安培; nh

Else

lblTemp.Text ="不需要调整大小。

结束如果

结束功能


这里是我的上传代码,缩写


如果picType =" image / jpeg"或者picType =" image / gif"或者picType =" image / pjpeg"或者picType =" image / bmp"然后

File.PostedFile.SaveAs(路径)

ResizeImage(路径,120,95)

lblError.Text ="扬声器照片上传成功!确保单击下面的更新以保存更改。

否则

lblError.Text ="无效的图像格式。只允许使用JPG,JPEG,GIF和BMP图像。

结束如果


再次,上传效果很好


谢谢!!


-

David Lozzi

Web应用程序开发人员
dlozzi @(删除 - 这个)delphi-ts.com


< blockquote>嗨David,


我们已经审查了这个问题,目前正在研究它。我们将尽快更新您的b $ b。感谢您的耐心等待!


Kevin Yu

=======

此帖已提供按原样没有保证,并且不授予

权利。


嘿大卫,


所以这似乎是一个纯粹的GDI +图像处理问题。是否会出现此问题

仅当上传的图像具有特定格式或者是上传到服务器上的任何图像时会出现的常见问题?通常我认为

我们可以通过以下步骤解决问题:


1.确保将图像正确上传到服务器上,打开它
图像查看器中的
以确保数据没有损坏。


2.然后,使用一些标准图像调整大小代码来调整图像大小...

这里是我从网上挑选的一些简单的GDI +代码,用于调整图像大小

到百分比值:


静态图像ScaleByPercent(图片imgPhoto,int百分比)

{

浮点数nPercent =((浮点数)百分比/ 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);


位图bmPhoto =新位图(destWidth,destHeight,

P ixelFormat.Format24bppRgb);

bmPhoto.SetResolution(imgPhoto.Horizo​​ntalResolutio n,

imgPhoto.VerticalResolution);


图形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();

返回bmPhoto;

}


您可以尝试调整大小代码也看看是否可以正常工作

....


如果还有其他任何发现,请随时在此发布。


谢谢,


Steven Cheng

微软在线支持


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)


------------ --------

|来自:David Lozzi < Da ******** @ nospam.nospam>

|参考文献:< #r ************** @ TK2MSFTNGP09.phx.gbl>

|主题:Re:上传图片然后调整大小 - 发生了一般性错误
GDI +中的


|日期:2005年12月9日星期五11:15:17 -0500

|行数:270

| MIME版本:1.0

|内容类型:multipart / alternative;

| boundary =" ---- = _ NextPart_000_0011_01C5FCB1.D5B1AFD0"

| X-Priority:3

| X-MSMail-Priority:正常

| X-Newsreader:Microsoft Outlook Express 6.00.2900.2670

| X-MimeOLE:由Microsoft MimeOLE制作V6.00.2900.2670

|消息ID:< #G ************** @ TK2MSFTNGP09.phx.gbl>

|新闻组:microsoft.public.dotnet.framework.aspnet

| NNTP-Posting-Host:c-24-63-42-200.hsd1.ma.comcast.net 24.63.42.200

|路径:TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl

|外翻:TK2MSFTNGXA02.phx.gbl

microsoft.public.dotnet.framework.aspnet:363864

| X-Tomcat-NG:microsoft.public.dotnet.framework.aspnet

|

|除了这一点,我只是意识到我的数学是错误的,请不要这么多b $ b批评。我只需要解决调整大小问题。

| -

| David Lozzi

| Web应用程序开发人员

| dlozzi@(remove-this)delphi-ts.com

| David Lozzi <沓******** @ nospam.nospam>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

|你好,

|我有一个上传图像的功能,效果很好。我喜欢

。网上内置,比第三方上传者容易得多!

|现在我正在创建一个公共函数,它将采用

上传图像的路径,并使用提供的尺寸调整其大小。我的功能是

以下。从上传运行时,当前函数返回错误

函数:GDI +中发生一般错误。不确定

究竟是什么意思。据我所知,没有人真正知道这意味着什么....这里是

我的公共职能

|函数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

|每小数点亮

|如果我没有>或者我是> h然后''检查是否需要调整大小

|如果w>然后''获得更大的维度并获得百分比

| per = iw / w

|否则

| per = ih / h

|结束如果

| ''根据百分比创建新尺寸

| nw = iw * per

| nh = ih * per

| ''现在保存它

|昏暗的尺寸为新尺寸(nw,nh)

| Dim nimg As New Bitmap(img,size)

| nimg.Save(p)

| lblTemp.Text ="< br> Old" &安培; iw&英寸×" &安培;呃& < br> New &安培; nw

&英寸×" &安培; nh

|否则

| lblTemp.Text =不需要调整大小。

|结束如果

|结束功能

|这是我的上传代码,缩写为

|如果picType =" image / jpeg"或者picType =" image / gif"或者

picType =" image / pjpeg"或者picType =" image / bmp"然后

| File.PostedFile.SaveAs(路径)

| ResizeImage(路径,120,95)

| lblError.Text =发布的扬声器照片

成功!确保单击下面的更新以保存更改。

|否则

| lblError.Text ="图片格式无效。只允许JPG,

JPEG,GIF和BMP图像。

|结束如果

|再次,上传效果很好

|谢谢!!

| -

| David Lozzi

| Web应用程序开发人员

| dlozzi@(remove-this)delphi-ts.com

|

|


Howdy,

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

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

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

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

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

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

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

and here is my code for the upload, abbreviated

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

Again, the upload works great

Thanks!!

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

解决方案

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


Hi David,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Hey David,

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

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

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

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

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

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

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.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
|
|


这篇关于上传图像然后调整大小 - GDI +中发生一般错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆