我有一个aspx页面,它将从客户端pc上传图像到服务器硬盘
但现在我需要改变我的程序,这样我就可以在上传时调整图像大小.
有没有人对此有任何想法?我无法使用输入文件服务器控件找到这样的属性/方法
那里有人指导我吗?
将文件保存到服务器后,您可以使用此类代码来调整大小.此代码将处理调整大小时的长/宽比.
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight) { System.Drawing.Bitmap bmpOut = null; try { Bitmap loBMP = new Bitmap(lcFilename); ImageFormat loFormat = loBMP.RawFormat; decimal lnRatio; int lnNewWidth = 0; int lnNewHeight = 0; if (loBMP.Width < lnWidth && loBMP.Height < lnHeight) return loBMP; if (loBMP.Width > loBMP.Height) { lnRatio = (decimal)lnWidth / loBMP.Width; lnNewWidth = lnWidth; decimal lnTemp = loBMP.Height * lnRatio; lnNewHeight = (int)lnTemp; } else { lnRatio = (decimal)lnHeight / loBMP.Height; lnNewHeight = lnHeight; decimal lnTemp = loBMP.Width * lnRatio; lnNewWidth = (int)lnTemp; } bmpOut = new Bitmap(lnNewWidth, lnNewHeight); Graphics g = Graphics.FromImage(bmpOut); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight); g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight); loBMP.Dispose(); } catch { return null; } return bmpOut; }
您将无法"动态"调整大小,因为在执行任何图像转换之前,您需要拥有完整的图像.但是,在上传完成后,在向用户显示任何结果之前,您可以使用我在几个应用程序中使用的基本图像大小调整方法:
'''''' Resize image with GDI+ so that image is nice and clear with required size. ''' ''' Image to resize ''' New height to resize to. ''' New width to resize to. '''Image object resized to new dimensions. '''Public Shared Function ImageResize(ByVal SourceImage As Image, ByVal NewHeight As Int32, ByVal NewWidth As Int32) As Image Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(NewWidth, NewHeight, SourceImage.PixelFormat) If bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format1bppIndexed Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format4bppIndexed Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format8bppIndexed Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Undefined Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.DontCare Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppArgb1555 Or _ bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppGrayScale Then Throw New NotSupportedException("Pixel format of the image is not supported.") End If Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap) graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic graphicsImage.DrawImage(SourceImage, 0, 0, bitmap.Width, bitmap.Height) graphicsImage.Dispose() Return bitmap End Function