博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

转 c#下分割图像的几种办法

Posted on 2008-12-26 23:16  烈火123  阅读(465)  评论(0编辑  收藏  举报
  1. Bitmap   bb   =   (Bitmap)Image.FromFile("...");   
  2.   Bitmap   sepbitmap1=   bb.Clone(new   System.Drawing.Rectangle(0,0,20,20),System.Drawing.Imaging.PixelFormat.Format32bppPArgb);   
  3.   
  4. Image   image   =   System.Drawing.Image.FromFile("a.jpg",true);   
  5.   Bitmap   bmp   =   new   Bitmap(400,400);   
  6.   Graphics   grap   =   Graphics.FromImage(bmp);   
  7.   grap.Clear(Color.Transparent);   
  8.   grap.DrawImage(image,new   Rectangle(0,0,400,400));   
  9.      如果a.jpg的大小是4000*4000,这样在新的图像中得到的就是左上角起的400*400的一部份.   
  10.   若要10*10,则用循环就可实现   
  11. [System.Runtime.InteropServices.DllImportAttribute   (   "gdi32.dll"   )   ]   
  12.   private   static   extern   bool   BitBlt   (   
  13.   IntPtr   hdcDest   ,   //   目标   DC的句柄   
  14.   int   nXDest   ,     
  15.   int   nYDest   ,     
  16.   int   nWidth   ,     
  17.   int   nHeight   ,     
  18.   IntPtr   hdcSrc   ,   //   源DC的句柄   
  19.   int   nXSrc   ,     
  20.   int   nYSrc   ,     
  21.   System.Int32   dwRop   //   光栅的处理数值   
  22.   )   ;   
  23.     
  24.   private   void   button1_Click(object   sender,   System.EventArgs   e)   
  25.   {   
  26.   try   
  27.   {   
  28.   string   file   =   null;   
  29.   Bitmap   bmp   =   null;   
  30.   IntPtr   dc1   =   IntPtr.Zero;   
  31.   IntPtr   dc2   =   IntPtr.Zero;   
  32.   int   row   =   2,   col   =   2;   
  33.     
  34.   int   left   =   0,   top   =0   ,   width   =   0,   height   =   0;   
  35.   for(int   i=0;   i<row;   i++)   
  36.   {   
  37.   left   =   0;   
  38.   top   =   i   *   ((int)(this.pictureBox1.Height   /   row));   
  39.   for(int   j=0;   j<col;   j++)   
  40.   {   
  41.   file   =   @"c:/"+   i.ToString()   +   j.ToString()   +".bmp";   
  42.   if   (j   ==   col-1)     
  43.   width   =   this.pictureBox1.Width   -   ((int)(this.pictureBox1.Width   /   col))   *   j;   
  44.   else   
  45.   width   =   (int)(this.pictureBox1.Width   /   col);   
  46.     
  47.   if   (i   ==   row-1)     
  48.   height   =   this.pictureBox1.Height   -   ((int)(this.pictureBox1.Height   /   row))   *   i;   
  49.   else   
  50.   height   =   (int)(this.pictureBox1.Height   /   row);   
  51.     
  52.                                                   Graphics   g1   =   this.pictureBox1.CreateGraphics();   
  53.   bmp   =   new   Bitmap(width,   height,   g1);   
  54.   Graphics   g2   =   Graphics.FromImage(bmp);   
  55.   dc1   =   g1.GetHdc();   
  56.   dc2   =   g2.GetHdc();   
  57.     
  58.     
  59.   BitBlt   (dc2   ,   0   ,   0   ,   width   ,   height   ,   dc1   ,   left   ,   top   ,   13369376   )   ;   
  60.   g1.ReleaseHdc(dc1)   ;   
  61.   g2.ReleaseHdc(dc2)   ;   
  62.   g1.Dispose();   
  63.   g2.Dispose();   
  64.   bmp.Save(file,   System.Drawing.Imaging.ImageFormat.Bmp);   
  65.     
  66.   left   +=   width;   
  67.     
  68.   }   
  69.   }   
  70.     
  71.   }   
  72.   catch(Exception   ex)   
  73.   {   
  74.   MessageBox.Show(ex.ToString());   
  75.   }   
  76.     
  77.   }