img 圖片文件操作c#


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.UI.HtmlControls;

namespace HuoLi.Common
{
 /// <summary>
 /// img 的摘要說明。
 /// </summary>
 public class img
 {
  public img()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }

  /// <summary>
  /// 圖片處理函數(生成小圖)
  /// </summary>
  /// <param name="pic_path">原始圖片路徑</param>
  /// <param name="pic_path0">要生成的小圖片的路徑</param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  public static void pic_0(string pic_path,string pic_path0,int width,int height)
  {
   System.Drawing.Image img= System.Drawing.Image.FromFile(pic_path);
   int c_width=width;
   int c_height=height;
   int width1=img.Width;
   int height1=img.Height;
   int min=(c_width>=c_height?c_height:c_width);
   if(width1==height1)
   {
    c_width=c_height=min;
   }   
   else
   {
    if(width1>height1)
    {
     c_height=c_width*height1/width1;
     if(c_height>height)
     {
      c_height=height;
      c_width=c_height*width1/height1;
     }
    }
    else
    {
     c_width=c_height*width1/height1;
     if(c_width>width)
     {
      c_width=width;    
      c_height=c_width*height1/width1;
     }
    }
   }
   Bitmap bm=new Bitmap(c_width,c_height);
   Graphics g;
   g = Graphics.FromImage (bm) ;
   g.Clear(Color.White);
   //對選中的原始圖片區域進行拖放,拖放到width,height大小
   Rectangle rec=new Rectangle(0,0,c_width,c_height);
   //選中原始圖片的區域,[0,0,width1,height1]為選中塊的坐標,可以畫圖軟件看到效果
   g.DrawImage(img,rec,0,0,width1,height1,GraphicsUnit.Pixel );
   bm.Save(pic_path0, ImageFormat.Jpeg);
   img.Dispose();
   g.Dispose();
   bm.Dispose();
  }

  /// <summary>
  /// 上傳文件並生成小圖片,刪除原始圖片
  /// </summary>
  /// <param name="File1"></param>
  /// <param name="SaveFileFolder">保存文件夾的路徑,如: "UpFiles/"</param>
  /// <returns>小圖片的相對存放路徑 string</returns>
  public static string SaveSmallImages(HtmlInputFile File1,string SaveFileFolder)
  {
   string p = string.Empty;
   //首頁圖片定義
   if (File1.Value!="")
   {
    string fv = File1.Value;
    string ext = System.IO.Path.GetExtension(fv);
    string newNameB = Guid.NewGuid().ToString("n");
    string oldNameAll = newNameB+ext;
    string newNameAll = "s"+newNameB+ext;
    string smOldPath = System.Web.HttpContext.Current.Server.MapPath(SaveFileFolder)+oldNameAll;
    string smNewPath = System.Web.HttpContext.Current.Server.MapPath(SaveFileFolder)+newNameAll;
    string smSaveNewPath = SaveFileFolder + newNameAll;
    if (System.IO.File.Exists(smOldPath))
    {
     Location.MsgBox("已存在相同文件名,請重新上傳JPG圖片!");
    }
    else
    {
     ReadXML rdxml = new ReadXML();
     string smFolder = System.Web.HttpContext.Current.Server.MapPath("UpFiles/");
     if (!System.IO.Directory.Exists(smFolder))
     {
      System.IO.Directory.CreateDirectory(smFolder);
     }
     File1.PostedFile.SaveAs(smOldPath);
     pic_0(smOldPath,smNewPath,rdxml.smallPicWidth,rdxml.smallPicHeight); //生成首頁小圖片;
     p = smSaveNewPath;
     if (System.IO.File.Exists(smOldPath))
     {
      System.IO.File.Delete(smOldPath);
     }
    }
   }
   return p;
  }
 }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM