C#圖像處理2

1.圖像的複製、剪切和粘貼

Clipboard類由Object類派生而來,在應用程序中可以直接調用ClipBoard類,或者構建剪貼板實例來操作。

獲取剪貼板的內容:IDataObject iData=System.WinForms.Clipboard.GetDataObject();

複製:setDataObject(str);           setDataObject(str,true);//表示應用程序退出後數據依然保留在剪貼板

粘貼:IDataObject iData=System.WinForms.Clipboard.GetDataObject();

          if(iData.GetDataPresent(DataFormats.Text)){…}

圖像的複製:

        /// <summary>
        /// 圖像的複製
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void 複製圖像ToolStripMenuItemClick(object sender, EventArgs e)
        {
        	Clipboard.SetDataObject(pictureBox1.Image,true);
        }

圖像的剪切:

 void ClipMenuClick(object sender, EventArgs e)
        {
        	Clipboard.SetDataObject(pictureBox1.Image,true);
        	pictureBox1.Image=null;
        }

圖像的粘貼:

void PasteMenuClick(object sender, EventArgs e)
      {
          IDataObject iData=Clipboard.GetDataObject();
          if (iData.GetDataPresent(DataFormats.Bitmap) ){
                  pictureBox1.Image=(Bitmap)iData.GetData(DataFormats.Bitmap);
          }
      }
圖像平滑處理:    
         void SmoothMenuClick(object sender, EventArgs e)
  34:         {
  35:             Bitmap bmp=new Bitmap(pictureBox1.Image);
                   Bitmap newbmp=new Bitmap(pictureBox1.Image);
  37:             Color c=new Color();
  38:             Color NewC;
  39:             Byte r,g,b;
  40:             int r2,g2,b2;
  41:             
  42:             for (int i=1;i<bmp.Width-1 ;i++ )
  43:             {
  44:                 for (int j=1; j<bmp.Height-1; j++) {
  45:                     r2=0;g2=0;b2=0;
  46:                     for (int k1=-1; k1<=1; k1++) {
  47:                         for (int k2=-1; k2<=1; k2++) {
  48:                             c=bmp.GetPixel(i+k1,j+k2);
  49:                             r=c.R;
  50:                             g=c.G;
  51:                             b=c.B;
  52:                             
  53:                             r2=r2+r;
  54:                             g2=g2+g;
  55:                             b2=b2+b;
  56:                         }
  57:                     }
  58:                     
  59:                     r2=(Byte)(r2/9);
  60:                     g2=(Byte)(g2/9);
  61:                     b2=(Byte)(b2/9);
  62:                     NewC=Color.FromArgb(r2,g2,b2);
  63:                     newbmp.SetPixel(i,j,NewC);
  64:                 }
  65:             }
  66:             
  67:             // 刷新顯示
  68:             pictureBox2.Refresh();
  69:             pictureBox2.Image=newbmp;
70:         }
 
梯度圖像
  73:         /// 首先計算源圖像的像素(i, j)點的分量值與同行的下一個像素
  74:         /// (i+1, j)以及相同列的下一個像素(i, j+1)的分量的梯度,
  75:         /// 即差的平方和的平方根,然後將梯度值作爲新的像素重新放回(i, j)
  76:         /// </summary>
  77:         void NihongMenuClick(object sender, EventArgs e)
  78:         {
  79:             Bitmap bmp=new Bitmap(pictureBox1.Image);
  80:             Bitmap newbmp=new Bitmap(pictureBox1.Image);
  81:             Color c1=new Color();
  82:             Color NewC;
  83:             int r1,r2,r3,g1,g2,g3,b1,b2,b3,rr,gg,bb;
  84:             
  85:             for (int i=0;i<bmp.Width;i++ )
  86:             {
  87:                 for (int j=0; j<bmp.Height; j++) {
  88:                     c1 = bmp.GetPixel(i, j);
  89:                     r1 = c1.R;
  90:                     g1 = c1.G;
  91:                     b1 = c1.B;
  92:                     if (i+1==bmp.Width) {
  93:                         c1 = bmp.GetPixel(i , j);
  94:                     }
  95:                     else
  96:                     {
  97:                         c1 = bmp.GetPixel(i + 1, j);
  98:                     }
  99:                     r2 = c1.R;
 100:                     g2 = c1.G;
 101:                     b2 = c1.B;
 102:                     if (j+1==bmp.Height) {
 103:                         c1 = bmp.GetPixel(i , j);
 104:                     }
 105:                     else
 106:                     {
 107:                         c1 = bmp.GetPixel(i, j+1);
 108:                     }
 109:                     r3 = c1.R;
 110:                     g3 = c1.G;
 111:                     b3 = c1.B;
 112:                     rr = (int)(255-2 * Math.Sqrt((r1 - r2) * (r1 - r2) + (r1 - r3) * (r1 - r3)));
 113:                     gg = (int)(255-2 * Math.Sqrt((g1 - g2) * (g1 - g2) + (g1 - g3) * (g1 - g3)));
 114:                     bb = (int)(255-2 * Math.Sqrt((b1 - b2) * (b1 - b2) + (b1 - b3) * (b1 - b3)));
 115:                     NewC=Color.FromArgb((Byte)rr,(Byte)gg,(Byte)bb);
 116:                     newbmp.SetPixel(i,j,NewC);
 117:                 }
 118:             }
 119:             
 120:             // 刷新顯示
 121:             pictureBox2.Refresh();
 122:             pictureBox2.Image=newbmp;
 123:         }
 

如何讓窗體畫圖時,可以根據圖片大小自動縮放:

if (curBitmap != null)

    {

                Graphics g = e.Graphics;

               // g.InterpolationMode = InterpolationMode.Bicubic;

                Brush fillbrush=Brushes.White;

                //判斷圖片的大小,如果比窗體大,則進行縮放,否則按照原始大小顯示

                if (curBitmap.Width > formWidth || curBitmap.Height > formHeight)

                {

                    //圖片大小大於窗體 設置窗體大小

                    this.Width = curBitmap.Width;

                    this.Height = curBitmap.Height;

                    Rectangle formRect = new Rectangle(0,0,this.Width,this.Height);

                    g.FillRectangle(fillbrush, formRect);

                    g.DrawImage(curBitmap, formRect);

                }

                else

                {

                    //圖片大小小於窗體

                    this.Width = 800;

                    this.Height = 600;

                    Rectangle formRect = new Rectangle(0, 0, formWidth, formHeight);

                    g.FillRectangle(fillbrush, formRect);

                    g.DrawImage(curBitmap, 0, 0, curBitmap.Width, curBitmap.Height);

 

                }

設置窗體自動出現滾動條

if(DialogResult.OK == openFileDialog.ShowDialog())

 {

   m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);

   this.AutoScroll = true;

   this.AutoScrollMinSize=new Size ((int)(m_Bitmap.Width),(int)

                m_Bitmap.Height));

   this.Invalidate();

 }

程序風格:

最好將對圖像進行處理的操作如:反色、灰度化、加亮等寫成函數,將它們作爲一個圖像操作類的靜態函數(static),返回值爲bool型,

根據返回值決定窗體是否重繪。

public static bool Brightness(Bitmap b, int nBrightness)

 {

  if (nBrightness < -255 || nBrightness > 255)

   return false;

  BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width,

                                    b.Height), ImageLockMode.ReadWrite,

                                    PixelFormat.Format24bppRgb);

  int stride = bmData.Stride;

  System.IntPtr Scan0 = bmData.Scan0;

  int nVal = 0;

  unsafe

  {

   byte * p = (byte *)(void *)Scan0;

   int nOffset = stride - b.Width*3;

   int nWidth = b.Width * 3;

   for(int y=0;y<b.Height;++y)

   {

      for(int x=0; x < nWidth; ++x )

       {

    nVal = (int) (p[0] + nBrightness);

    if (nVal < 0) nVal = 0;

    if (nVal > 255) nVal = 255;

    p[0] = (byte)nVal;

    ++p;

        }

       p += nOffset;

   }

  }

  b.UnlockBits(bmData);

  return true;

 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章