请楼主参见上传图片并生成略缩图 
参数说明
FileBox:上传表单中的表单名
SavePath:保存路径
ThumbnailWidth:略缩图宽
ThumbnailHeight:略缩图高
FilePath:文件保存路径(输出参数)
FileName:文件名称(输出参数)
LastError:(输出错误)
public static bool UploadImages(System.Web.UI.HtmlControls.HtmlInputFile FileBox,string SavePath,Int32 ThumbnailWidth,Int32 ThumbnailHeight,out String FilePath,out String FileName,out string LastError)
{
DateTime datTime=System.DateTime.Now;
String strTemp;
string strFileName = FileBox.PostedFile.FileName;
bool blnResult;
LastError="";
System.Drawing.Image xImage;
System.Drawing.Bitmap xBitmap;
int PhotoHeight,PhotoWidth;
string BasePath=System.Web.HttpContext.Current.Server.MapPath("../");
Rectangle NewPhoto;
System.Drawing.Imaging.ImageFormat xObject;
string Extension="";
FileName="";
FilePath="";try
{
if(strFileName=="")
{
LastError="请点击浏览选择要上传的文件!";
return false;
}
if(Convert.ToDecimal(FileBox.PostedFile.ContentLength)<= 2000000 &&Convert.ToDecimal(FileBox.PostedFile.ContentLength)>1500)
{
//查看扩展名是否合法
Extension=Path.GetExtension(strFileName);
Extension=Extension.ToLower();
if(Extension==".gif" || Extension==".jpg" || Extension==".jpeg" || Extension==".bmp")
{
switch(Extension)
{
case ".gif":
xObject=System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".bmp":
xObject=System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
xObject=System.Drawing.Imaging.ImageFormat.Jpeg;break;
}
}
else
{
LastError="上传的文件不是有效的图片文件,请上传格式为:.gif.jpg.bmp的文件!";
return false;
}
//-----------------------------
//生成保存的文件路径及文件名
//-----------------------------
strTemp = datTime.ToShortDateString().Replace( "-", "");
FileName = datTime.ToLongTimeString().Replace(":","")+ Extension;
FilePath = SavePath + strTemp + "/";
Friendlib.Function.CreateDirectory(BasePath + FilePath);
//保存大图
xImage=System.Drawing.Bitmap.FromStream(FileBox.PostedFile.InputStream);xImage.Save(BasePath+FilePath+FileName);
xImage.Dispose();
//-----------------------------
//开始生成缩略图
//-----------------------------
xBitmap=new Bitmap(BasePath+FilePath+FileName);//------------------PhotoHeight=xBitmap.Height;
PhotoWidth=xBitmap.Width;
// //判断图片的形状 
if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)>Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))//高有余 

PhotoHeight=Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))*Convert.ToDecimal(PhotoWidth)); 
NewPhoto=new Rectangle(0,0,PhotoWidth,PhotoHeight); 

else if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)<Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth)) 

PhotoWidth=Convert.ToInt16((Convert.ToDecimal(ThumbnailWidth)/Convert.ToDecimal(ThumbnailHeight))*Convert.ToDecimal(PhotoHeight)); 
NewPhoto=new 
Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width)-Convert.ToDecimal(PhotoWidth))/2),0,PhotoWidth,PhotoHeight); 

else 

NewPhoto= newRectangle(0,0,PhotoWidth,PhotoHeight); < BR> } 
System.Drawing.Image myBitmap; 
myBitmap=xBitmap.Clone(NewPhoto,System.Drawing.Imaging.PixelFormat.DontCare); 
System.Drawing.Image.GetThumbnailImageAbort myCallback = new 
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 
System.Drawing.Image myThumbnail = 
myBitmap.GetThumbnailImage(ThumbnailWidth,ThumbnailHeight, myCallback, 
IntPtr.Zero); myThumbnail.Save(BasePath + FilePath +"s"+FileName,xObject); 
myThumbnail.Dispose(); myBitmap.Dispose(); xBitmap.Dispose(); return true; } 
else 

blnResult=false; LastError="不能上传大于2M及小于1.5K的图片!"; 

}< BR> catch (Exception ex) { 
throw new System.Exception((ex.Message + ("\r\n" + ex.StackTrace))); 

return blnResult;
}
private static bool ThumbnailCallback() { return false; }

解决方案 »


  1.  
     
    缩略图水印组件wsImage3.5  
      

  2. octverve(生命无色,命运多彩……) 
    这位的方法已经很好了
      

  3. Private Function getimg(ByRef bigimg As String, ByRef smallimg As String, ByVal smallpath As String, ByVal bigpath As String, ByVal simagefile As String, ByVal file1 As String, ByVal upimgfile As Stream)            Dim g As Graphics = Nothing
                Dim upimage As System.Drawing.Image = Nothing
                Dim thumimg As System.Drawing.Image = Nothing
                Dim simage As System.Drawing.Image = Nothing
                Dim outputfile As Bitmap = Nothing
                Try
                    Dim extension As String = Path.GetExtension(file1).ToUpper
                    Dim filename As String = DateTime.Now.ToString("yyyyMMddhhmmss")                Dim width As Integer
                    Dim height As Integer
                    Dim newwidth As Integer
                    Dim newheight As Integer
                    Dim callb As System.Drawing.Image.GetThumbnailImageAbort = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
                    If Not Directory.Exists(smallpath) Then
                        Directory.CreateDirectory(smallpath)
                    End If
                    If Not Directory.Exists(bigpath) Then
                        Directory.CreateDirectory(bigpath)
                    End If
                    simage = System.Drawing.Image.FromFile(simagefile)
                    upimage = System.Drawing.Image.FromStream(upimgfile)
                    width = upimage.Width
                    height = upimage.Height
                    If width > height Then
                        newwidth = 100
                        newheight = CType((CType(height, Double) / CType(width, Double) * CType(newwidth, Double)), Integer)
                    Else
                        newheight = 100
                        newwidth = CType((CType(width, Double) / CType(height, Double) * CType(newheight, Double)), Integer)
                    End If
                    thumimg = upimage.GetThumbnailImage(newwidth, newheight, callb, IntPtr.Zero)
                    outputfile = New Bitmap(upimage)
                    g = Graphics.FromImage(outputfile)
                    g.DrawImage(simage, New Rectangle(upimage.Width - simage.Width, upimage.Height - simage.Height, upimage.Width, upimage.Height), 0, 0, upimage.Width, upimage.Height, GraphicsUnit.Pixel)
                    Dim newpath As String = bigpath + filename + extension
                    Dim thumpath As String = smallpath + filename + extension
                    bigimg = "admin/pro/" + filename + extension
                    smallimg = "admin/smallimg/" + filename + extension
                    outputfile.Save(newpath)
                    thumimg.Save(thumpath)
                    outputfile.Dispose()            Catch ex As Exception
                    Throw ex
                Finally
                    If Not (g Is Nothing) Then
                        g.Dispose()
                    End If
                    If Not (thumimg Is Nothing) Then
                        thumimg.Dispose()
                    End If
                    If Not (upimage Is Nothing) Then
                        upimage.Dispose()
                    End If
                    If Not (simage Is Nothing) Then
                        simage.Dispose()
                    End If
                End Try
            End Function
      

类似问题 »