2012年9月27日 星期四

圖片縮圖

3002/30025.aspx 負責處理圖片縮圖

' 取得圖型資料及縮圖處理   
' FileUpload 的檔案內容存入 Image    
Using img_tmp As System.Drawing.Image = System.Drawing.Image.FromFile(fullname)   
 ac_height = img_tmp.Height  
 ' 實際高度   
 ac_width = img_tmp.Width  
 ' 實際寬度   
 ' 維持圖檔比例的方式,計算與縮圖 120 * 120 的比例   
 If ac_width > ac_height Then  
  fCnt = ac_width / 120.0R 
 Else  
  fCnt = ac_height / 120.0R 
 End If  
   
 ' 實際圖比縮圖大時才要處理,否則仍為原圖檔尺寸   
 If fCnt > 1 Then  
  s_width = CInt((ac_width / fCnt)) 
  ' 縮圖寬度  
  ' 縮圖高度  
  s_height = CInt((ac_height / fCnt)) 
 Else  
  s_width = ac_width 
  ' 縮圖寬度  
  ' 縮圖高度  
  s_height = ac_height 
 End If  
   
 ' 呼叫 Bitmap 物件的 GetThumbnailImage 方法來建立一個縮圖  
 Using img_thumb As System.Drawing.Image = img_tmp.GetThumbnailImage(s_width, s_height, Nothing, IntPtr.Zero)  
  fullname = lb_path.Text & "_thumb\" & fname & ".jpg" 
   
  ' 縮圖的壓縮比為 75%  
  Dim eps As New EncoderParameters() 
  eps.Param(0) = New EncoderParameter(Encoder.Quality, CLng(75)) 
   
   
  ' 以預設壓縮比儲存 jpeg (75%)  
  ' img_thumb.Save(fullname, System.Drawing.Imaging.ImageFormat.Jpeg);  
  img_thumb.Save(fullname, GetEncoderInfo("image/jpeg"), eps) 
 End Using  
   
End Using   



-------------------------------------------------------
'網路上找到的資料


using   System;
using   System.Collections.Specialized;
using   System.Drawing;
using   System.Drawing.Imaging;
using   System.IO;
using   System.Data;
using   System.Configuration;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   System.Web.Configuration;
using   Microsoft.Web.UI;
public   partial   class   _Default   :   System.Web.UI.Page  
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
        }
        protected   void   ItemUpdating(object   sender,   IOrderedDictionary   newData,   IOrderedDictionary   oldData)
        {
                try
                {
                        FileUpload   fileUpload   =   (FileUpload)((DetailsView)sender).FindControl( "FileUpload ");
                        //e.Values.
                        if   (fileUpload.HasFile)
                        {
                                newData[ "MIME_TYPE "]   =   Server.HtmlEncode(fileUpload.PostedFile.ContentType);
                                newData[ "FILESIZE "]   =   fileUpload.PostedFile.ContentLength;
                                newData[ "FILENAME "]   =   Server.HtmlEncode(Path.GetFileName(fileUpload.PostedFile.FileName));
                                byte[]   b   =   fileUpload.FileBytes;
                                byte[]   bThumb   =   CreateThumbnail(b);
                                newData[ "THUMBNAIL "]   =   bThumb;
                                newData[ "ASSETFILE "]   =   b;
                                return;
                        }
                        if(oldData!=null)
                        {
                                newData[ "MIME_TYPE "]   =   oldData[ "MIME_TYPE "];
                                newData[ "FILESIZE "]   =     oldData[ "FILESIZE "]   ;
                                newData[ "FILENAME "]   =     oldData[ "FILENAME "]   ;
                                DictationDataSet   ds   =   new   DictationDataSet();
                                DictationDataSetTableAdapters.DICTATIONTableAdapter   da   =   new   DictationDataSetTableAdapters.DICTATIONTableAdapter();   ;
                                da.Fill(ds.DICTATION);
                                DataRow[]   drs   =   ds.DICTATION.Select( "ID= "   +   ((DetailsView)sender).DataKey.Value.ToString());
                                DataRow   dr   =   drs[0];
                                newData[ "THUMBNAIL "]   =   (byte[])dr[ "THUMBNAIL "];
                                newData[ "ASSETFILE "]   =   (byte[])dr[ "ASSETFILE "];
                        }
                }
                catch   (Exception   ex)
                {
                        throw   ex;
                }              
        }
        protected   void   DetailsView_ItemUpdating(object   sender,   DetailsViewUpdateEventArgs   e)
        {
                ItemUpdating(sender,   e.NewValues,e.OldValues);
        }
        protected   void   DetailsView_ItemInserting(object   sender,   DetailsViewInsertEventArgs   e)
        {
                ItemUpdating(sender,   e.Values,null);
        }
        static   public   byte[]   CreateThumbnail(byte[]   src)
        {
                try  
        {                
                        System.Drawing.Image   img   =   System.Drawing.Image.FromStream(new   MemoryStream(src));
                        System.Drawing.Image   imgthumb   =   img.GetThumbnailImage(100,   75,   null,   new   System.IntPtr(0));
                        MemoryStream   thumbstream=new   MemoryStream();
                        imgthumb.Save(thumbstream,   ImageFormat.Jpeg);
                        img.Dispose();
                        byte[]   b=thumbstream.ToArray();
                        imgthumb.Dispose();
                        return   b;
        }
        catch   (Exception   e)
        {
       
        }
                return   null;
        }
        protected   void   DetailsView_ItemInserted(object   sender,   DetailsViewInsertedEventArgs   e)
        {
                RebindGrid();
        }
        protected   void   DetailsView_ItemUpdated(object   sender,   DetailsViewUpdatedEventArgs   e)
        {
                RebindGrid();  
        }
        protected   void   DetailsView_ItemDeleted(object   sender,   DetailsViewDeletedEventArgs   e)
        {
                RebindGrid();  
        }
        void   RebindGrid()  
        {
                this.GridView.DataBind();
        }
        protected   void   GridView_SelectedIndexChanging(object   sender,   GridViewSelectEventArgs   e)
        {
                this.DetailsView.ChangeMode(DetailsViewMode.Edit);
        }
        protected   void   CustomValidator1_ServerValidate(object   source,   ServerValidateEventArgs   args)
        {
                FileUpload   fileUpload   =   (FileUpload)((DetailsView)this.DetailsView).FindControl( "FileUpload ");
                if   (fileUpload.HasFile)
                {
                        HttpRuntimeSection   config1   =   new   HttpRuntimeSection();
                        int   num1   =   (config1   !=   null)   ?   config1.MaxRequestLength   :   0x400000;
                        args.IsValid   =   (fileUpload.PostedFile.ContentLength   >   num1);
                }
        }
}

-------------------------------------------------------
'網路上找到的資料
'有一个vb的
'生成缩略图
Sub   MakeThumbnail(ByVal   originalImagePath   As   String,   ByVal   thumbnailPath   As   String,   ByVal   width   As   Integer,   ByVal   height   As   Integer,   ByVal   mode   As   String)
                Dim   originalImage   =   System.Drawing.Image.FromFile(originalImagePath)
                Dim   towidth   As   Integer   =   width
                Dim   toheight   As   Integer   =   height
                Dim   x   As   Integer   =   0
                Dim   y   As   Integer   =   0
                Dim   ow   As   Integer   =   originalImage.Width
                Dim   oh   As   Integer   =   originalImage.Height
                Select   Case   (mode)
                        Case   "HW "   '指定高宽缩放(可能变形)                                
                        Case   "W "   '指定宽,高按比例                                        
                                toheight   =   originalImage.Height   *   width   /   originalImage.Width
                        Case   "H "   '指定高,宽按比例
                                towidth   =   originalImage.Width   *   height   /   originalImage.Height
                        Case   "Cut "   '指定高宽裁减(不变形)                                
                                If   (CType(originalImage.Width,   Double)   /   CType(originalImage.Height,   Double))   >   (CType(towidth,   Double)   /   CType(toheight,   Double))   Then
                                        oh   =   originalImage.Height
                                        ow   =   originalImage.Height   *   towidth   /   toheight
                                        y   =   0
                                        x   =   (originalImage.Width   -   ow)   /   2
                                Else
                                        ow   =   originalImage.Width
                                        oh   =   originalImage.Width   *   height   /   towidth
                                        x   =   0
                                        y   =   (originalImage.Height   -   oh)   /   2
                                End   If
                End   Select

                '新建一个bmp图片
                Dim   bitmap   =   New   System.Drawing.Bitmap(towidth,   toheight)
                '新建一个画板
                Dim   g   =   System.Drawing.Graphics.FromImage(bitmap)
                '设置高质量插值法
                g.InterpolationMode   =   System.Drawing.Drawing2D.InterpolationMode.High
                '设置高质量,低速度呈现平滑程度
                g.SmoothingMode   =   System.Drawing.Drawing2D.SmoothingMode.HighQuality
                '清空画布并以透明背景色填充
                g.Clear(System.Drawing.Color.Transparent)
                '在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(originalImage,   New   System.Drawing.Rectangle(0,   0,   towidth,   toheight),   New   System.Drawing.Rectangle(x,   y,   ow,   oh),   System.Drawing.GraphicsUnit.Pixel)
                Try
                        '以jpg格式保存缩略图
                        bitmap.Save(thumbnailPath,   System.Drawing.Imaging.ImageFormat.Jpeg)
                Catch   ex   As   Exception
                        Throw   ex
                Finally
                        originalImage.Dispose()
                        bitmap.Dispose()
                        g.Dispose()
                End   Try
        End   Sub



後來網路上又找到呼叫另外軟體來做縮圖的
http://www.dotblogs.com.tw/killysss/archive/2011/06/20/29257.aspx?fid=30793#feedback

4 則留言:

  1. This is a very nice post which has been worthy my time. I really enjoyed how amazingly you noticed the micro changes and wrote them in broader way. Masters In Sociology Assignments

    回覆刪除
  2. Laptops have made lives of many people especially engineers, students, photographers, and teachers among other professions. This is attributed to the versatile features possessed by the laptop and its ability to be moved from one place with another. Also, students and researchers use the laptops to access quality online services such as Reviewing Services.

    回覆刪除
  3. Tithon and its rings (Tithon) - Titanium Arts
    Titanium earrings are used as gifts titanium wok for Tithon 안전한 바카라 사이트 and its rings (Tithon) and titanium plate flat iron the trekz titanium Tithon Ring. The Tithon Ring of Wisdom is the one with more rings than any titanium plate flat irons other Ring $24.95 · ‎In stock

    回覆刪除