C#实现自动图片切分,生成HTML

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.IO;
using  System.Drawing;
using  System.Collections;
using  System.Drawing.Imaging;

public   class  CropImageManipulator
{
    
public  CropImageManipulator()
    {

    }

    
//  不含扩展名的文件名
     private   string  _fileNameWithoutExtension;
    
//  文件扩展名
     private   string  _fileExtension;
    
//  文件所属的文件夹
     private   string  _fileDirectory;
   
    
///   <summary>
    
///  切分图片
    
///   </summary>
    
///   <param name="inputImgPath"></param>
    
///   <param name="cropWidth"></param>
    
///   <param name="cropHeight"></param>
    
///   <returns></returns>
     public   string  Cropping( string  inputImgPath,  int  cropWidth,  int  cropHeight)
    {
        
this ._fileNameWithoutExtension  =  System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
        
this ._fileExtension  =  System.IO.Path.GetExtension(inputImgPath);
        
this ._fileDirectory  =  System.IO.Path.GetDirectoryName(inputImgPath);

        
//  装载要分隔的图片
        Image inputImg  =  Image.FromFile(inputImgPath);
        
int  imgWidth  =  inputImg.Width;
        
int  imgHeight  =  inputImg.Height;

        
//  计算要分几格
         int  widthCount  =  ( int )Math.Ceiling((imgWidth  *   1.00 /  (cropWidth  *   1.00 ));
        
int  heightCount  =  ( int )Math.Ceiling((imgHeight  *   1.00 /  (cropHeight  *   1.00 ));
        
// ----------------------------------------------------------------------
        ArrayList areaList  =   new  ArrayList();

        System.Text.StringBuilder sb 
=   new  System.Text.StringBuilder();
        
// sb.Append("<table cellpadding='0' cellspacing='0' border='[$border]'>");
        sb.Append( " <table cellpadding='0' cellspacing='0' border='0'> " );
        sb.Append(System.Environment.NewLine);

        
int  i  =   0 ;
        
for  ( int  iHeight  =   0 ; iHeight  <  heightCount; iHeight ++ )
        {
            sb.Append(
" <tr> " );
            sb.Append(System.Environment.NewLine);
            
for  ( int  iWidth  =   0 ; iWidth  <  widthCount; iWidth ++ )
            {
                
string  fileName  =   string .Format( " <img src='images/{0}_{1}{2}' /> " this ._fileNameWithoutExtension, i,  this ._fileExtension);

                sb.Append(
" <td> "   +  fileName  +   " </td> " );
                sb.Append(System.Environment.NewLine);


                
int  pointX  =  iWidth  *  cropWidth;
                
int  pointY  =  iHeight  *  cropHeight;
                
int  areaWidth  =  ((pointX  +  cropWidth)  >  imgWidth)  ?  (imgWidth  -  pointX) : cropWidth;
                
int  areaHeight  =  ((pointY  +  cropHeight)  >  imgHeight)  ?  (imgHeight  -  pointY) : cropHeight;
                
string  s  =   string .Format( " {0};{1};{2};{3} " , pointX, pointY, areaWidth, areaHeight);

                Rectangle rect 
=   new  Rectangle(pointX, pointY, areaWidth, areaHeight);
                areaList.Add(rect);
                i
++ ;
            }
            sb.Append(
" </tr> " );
            sb.Append(System.Environment.NewLine);
        }

        sb.Append(
" </table> " );


        
// ----------------------------------------------------------------------    

        
for  ( int  iLoop  =   0 ; iLoop  <  areaList.Count; iLoop ++ )
        {
            Rectangle rect 
=  (Rectangle)areaList[iLoop];
            Directory.CreateDirectory(
this ._fileDirectory  +   " images/ " );
            
string  fileName  =   this ._fileDirectory  +   " images/ "   +   this ._fileNameWithoutExtension  +   " _ "   +  iLoop.ToString()  +   this ._fileExtension;
            Bitmap newBmp 
=   new  Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            Graphics newBmpGraphics 
=  Graphics.FromImage(newBmp);
            newBmpGraphics.DrawImage(inputImg, 
new  Rectangle( 0 0 , rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
            newBmpGraphics.Save();
            
switch  ( this ._fileExtension.ToLower())
            {
                
case   " .jpg " :
                
case   " .jpeg " :
                    newBmp.Save(fileName, ImageFormat.Jpeg);
                    
break ;
                
case   " gif " :
                    newBmp.Save(fileName, ImageFormat.Gif);
                    
break ;
            }

        }
        inputImg.Dispose();
        
string  html  =  sb.ToString();
        
return  html;
    }
    
///   <summary>
    
///  文件保留到HTML文件
    
///   </summary>
    
///   <param name="path"></param>
    
///   <param name="contents"></param>
     public   void  SavaFileToHtml( string  path, string  contents)
    {
        File.WriteAllText(path, contents);
    }

}

 

         private   void  button1_Click( object  sender, EventArgs e)
        {
            CropImageManipulator cim 
=   new  CropImageManipulator();
           
string  str =  cim.Cropping( " c:/1.jpg " 500 500 );
           cim.SavaFileToHtml(
" c:/1.htm " , str);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值