C# 關於畫圖Graphics Bitmap image


關於GDI+ 的使用,就對點,線,面的畫的操作,圖像剪裁,縮放等等操作,了解各種常用的方法和屬性。

常用命名空間:System.Drawing;System.Drawing.Image;System.Drawing.Drawing2D;

Graphics類封裝了一個GDI+繪圖圖面,提供將對象繪制到顯示到設備的方法。Graphics叫畫板,只不過這個畫板中帶了很多工具。但畫圖時你要定義畫板的大小,顏色等等,還應給他一張畫紙;

Graphics

1.創建Graphics基本方法:

  Graphics g = this.CreateGraphics();

  Graphics g = e;// Paint事件中的

  Graphics g = Graphics.FromImage();//Graphics.Fromxx類的各種靜態方法。

誰創建Graphics對象,就在誰上畫。

2.畫的方法:

   g.Drawxx 的各種方法。

3.Graphics用的 畫筆和畫刷

    pen 和 Font

        pen.PenType //屬性

        pen.DashStyle

     Font f = new Font( "宋體", 15, FontStyle.Bold | FontStyle.Italic );

    Brush //畫刷

      派生類:

            LinearGradientBrush//漸變畫刷

            SolidBrush//單色畫刷

            HatchBrush //用陰影樣式 (機械制圖時用的多)

            TextureBrush//畫字

            ImageBrush//圖片畫刷

            VisualBrush//

            RadialGradientBrush

            DrawingBrush

 4.圖片處理

      1. Graphics.SmoothingMode   //消除鋸齒常用
      2. Graphics.InterpolationMode  //圖像縮放常用
      3. Graphics.CompositingQuality  //

 5. clear()方法:

    Graphics g.clear(Color.Blue);// 不是清除xx顏色,是清除背景並設置xx顏色。

Bitmap ,image 和 Icon

        Bitmap bmp = new Bitmap(16, 16);
        Bitmap bmp1 = Bitmap.FromHbitmap(bmp.GetHbitmap());
        Image image = Image.FromFile(@"C:\\temp.jpg");
        bmp.MakeTransparent(Color.FromArgb(255, 0, 255));//把xx顏色設置透明色

          for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                     if (bmp.GetPixel(i, j) == Color.Blue)//獲取像素設置
                    {
                        bmp.SetPixel(i, j, Color.Red);
                    }
                }
            }

  ImageAttributes imageAttr = new ImageAttributes();//通過位圖和圖元文件顏色的信息設置顏色
  imageAttr.SetColorKey(lowerColor,upperColor, ColorAdjustType.Default);
  e.Graphics.DrawImage(Image, rect, 0, 0, 100, 100,  GraphicsUnit.Pixel, imageAttr);

  Stream IconStream = System.IO.File.OpenWrite(fileName);
  Bitmap bitmap = new Bitmap(pbImage.Image);
  bitmap.SetResolution(32, 32);
  Icon icon = System.Drawing.Icon.FromHandle(bitmap.GetHicon());
  icon.Save(IconStream);//比bitmap.save格式強點

 

其他常用:

        Clipboard.SetDataObject(this.pbSource.Image);//截圖
        IDataObject data = Clipboard.GetDataObject();
        if (data.GetDataPresent(DataFormats.Bitmap))
               image = (Bitmap)data.GetData(DataFormats.Bitmap);

        Color c = KnownColor.Control; 
        Color c  =SystemColors.Control
        Color c = Color.FromArgb(128, Color.Blue);  //128為半透明顏色 

 

 

         this.Opacity = 0.5//窗體的透明度

 

        System.Drawing.Drawing2D 命名空間下GraphicsPath

      //創建矢量圖
        Bitmap bmp = new Bitmap(220,220);
        Graphics g = Graphics.FromImage(bmp);
        Metafile mf  = new Metafile(filePath,g.GetHdc());
        //畫圖...
        g.Save();
        g.Dispose();
        mf.Dispose();

 防止圖片閃爍,雙緩沖設置
     SetStyle(ControlStyles.UserPaint |   
                  ControlStyles.AllPaintingInWmPaint | 
                  ControlStyles.OptimizedDoubleBuffer |
                  ControlStyles.ResizeRedraw |
                  ControlStyles.SupportsTransparentBackColor, true);

       盡量不要用窗體TransparencyKey否則閃爍和卡頓會使用閃爍更嚴重。

       不要在Paint事件給各種 xx.Image 賦值,xx.Image會調用paint這樣會死循環。
    圖像的各種效果(底片、浮雕、黑白、濾鏡)只是算法問題。


免責聲明!

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



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