2012-06-25 2 views

답변

0
  1. 당신은

    <asp:FileUpload ID="UploadFile" runat="server" /> 
    <asp:RegularExpressionValidator id="UpLoadValidator" runat="server" ErrorMessage="Upload Images only." ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.png|.bmp|.jpeg|.gif)$" ControlToValidate="FileUpload1"> </asp:RegularExpressionValidator> 
    
  2. 설정 이미지 최대 크기의 web.config에서 RegularExpressionValidator

    사용하여 간단하게 할 수

    <system.webServer> 
    <security> 
        <requestFiltering> 
        <requestLimits maxAllowedContentLength="524288"/> 
        </requestFiltering> 
    </security> 
    </system.webServer> 
    
+0

이미지의 크기를 고정하는 방법은 무엇입니까? 사용자가 우리의 크기로만 이미지를 업로드하도록 허용하십시오. –

0

파일이 이미지인지 확인하려면 확장명 (System.File.Path.GetExtension)을 확인하십시오. 당신은 쉽게 특정 크기를 초과 나던 이미지 크기를 조정하고 있는지 확인 할 수 있습니다

public static Bitmap CreateThumbnail(Bitmap loBMP, int lnWidth, int lnHeight) 
{ 
    System.Drawing.Bitmap bmpOut = null; 
    try 
    { 
     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.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight); 
     g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight); 
    } 
    catch 
    { 
     return null; 
    } 

    return bmpOut; 
} 
관련 문제