Asp.net 如何将图像保存到文件夹

Asp.net 如何将图像保存到文件夹,asp.net,vb.net,Asp.net,Vb.net,我正在使用webservice调用businesslogics中的方法(一个用vb编写的类)。我正在获取输入路径和路径,我必须在该方法中保存图像。我必须创建缩略图,还必须保存原始图像。这意味着我要将主图像保存在一个文件夹中,并将其缩略图保存在不同的文件夹中。我使用了以下代码 Public Function CreateThumbNails(ByVal intWidth As Integer, ByVal strInputFilePath As String, ByVal strFileName

我正在使用webservice调用businesslogics中的方法(一个用vb编写的类)。我正在获取输入路径和路径,我必须在该方法中保存图像。我必须创建缩略图,还必须保存原始图像。这意味着我要将主图像保存在一个文件夹中,并将其缩略图保存在不同的文件夹中。我使用了以下代码

Public Function CreateThumbNails(ByVal intWidth As Integer, ByVal strInputFilePath As String, ByVal strFileName As String, ByVal strOutputFilePath As String) As String
            Dim lnWidth As Integer = intWidth
            Dim lnHeight As Integer = 100
            Dim bmpOut As System.Drawing.Bitmap = Nothing
            Try
                Dim loBMP As New Bitmap(strInputFilePath)
                Dim lnRatio As Decimal
                Dim lnNewWidth As Integer = 0
                Dim lnNewHeight As Integer = 0
                If loBMP.Width < lnWidth AndAlso loBMP.Height < lnHeight Then
                    lnNewWidth = loBMP.Width
                    lnNewHeight = loBMP.Height
                End If
                If loBMP.Width > loBMP.Height Then
                    lnRatio = CDec(lnWidth) / loBMP.Width
                    lnNewWidth = lnWidth
                    Dim lnTemp As Decimal = loBMP.Height * lnRatio
                    lnNewHeight = CInt(lnTemp)
                Else
                    lnRatio = CDec(lnHeight) / loBMP.Height
                    lnNewHeight = lnHeight
                    Dim lnTemp As Decimal = loBMP.Width * lnRatio
                    lnNewWidth = CInt(lnTemp)
                End If

                ' *** This code creates cleaner (though bigger) thumbnails and properly
                ' *** and handles GIF files better by generating a white background for
                ' *** transparent images (as opposed to black)

                bmpOut = New Bitmap(lnNewWidth, lnNewHeight)
                Dim g As Graphics = Graphics.FromImage(bmpOut)
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)
                loBMP.Dispose()
                bmpOut.Save(HttpContext.Current.Server.MapPath(strOutputFilePath) + strFileName)
                bmpOut.Dispose()

                Return strOutputFilePath + strFileName
            Catch e As Exception

                Throw New Exception("ThumbNail Creation Failed")
                Return ""
            End Try
        End Function
Public函数创建缩略图(ByVal intWidth作为整数,ByVal strInputFilePath作为字符串,ByVal strFileName作为字符串,ByVal strOutputFilePath作为字符串)作为字符串
尺寸lnWidth为整数=整数宽度
尺寸高度为整数=100
Dim bmpOut As System.Drawing.Bitmap=无
尝试
将loBMP变暗为新位图(strInputFilePath)
以十进制表示的比率
Dim lnNewWidth为整数=0
Dim LNEWHEIGHT为整数=0
如果loBMP.WidthloBMP.Height,则
lnRatio=CDec(lnWidth)/loBMP.Width
lnNewWidth=lnWidth
尺寸LNTTEMP为十进制=叶高*LNTRATIO
lnNewHeight=CInt(lnTemp)
其他的
lnRatio=CDec(lnHeight)/loBMP.Height
lnNewHeight=lnHeight
尺寸LNTTEMP为十进制=叶宽*LNTRATIO
lnNewWidth=CInt(LNTEM)
如果结束
“***此代码创建更干净(尽管更大)的缩略图,并正确
'***并通过为其生成白色背景来更好地处理GIF文件
'***透明图像(与黑色相反)
bmpOut=新位图(lnNewWidth、lnNewHeight)
尺寸g作为图形=图形。来自图像(bmpOut)
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g、 FillRectangle(笔刷.White、0、0、lnNewWidth、lnNewHeight)
g、 DrawImage(loBMP、0、0、lnNewWidth、lnNewHeight)
loBMP.Dispose()
bmpOut.Save(HttpContext.Current.Server.MapPath(strOutputFilePath)+strFileName)
bmpOut.Dispose()
返回strOutputFilePath+strFileName
捕获e作为例外
抛出新异常(“缩略图创建失败”)
返回“”
结束尝试
端函数

我必须包含哪些代码才能将原始大小的图像保存到另一个文件夹中。有人能帮忙吗?

编辑。您不需要从位图中保存它。文件已经在那里了。只需复制文件

如果我理解您的问题,那么您希望在将图像操作到服务器上的新位置之前保存图像

该文件已作为文件存在于服务器上。该文件的文件位置作为参数(strInputFilePath)传递到函数中


最简单的方法是将文件复制到所需的位置。

为了保存原始图像,我必须在处理后编写什么代码GDI+中发生了一个一般性错误。该错误出现在loBMP.save(strMyOtherPath)行中。原因可能是什么?名为CreateThumbnails的方法真的是制作主图像副本的正确位置吗?您可能希望在其他地方执行此操作(或更改方法的名称)