Brush画图的使用 和 图片缩放

//实现功能:用Brush画图

//  1)创建的渐变色Brush(LinearGradientBrush,用完后应及时Dispose.)

//  2)Brushes绘图.(无须创建Brush)

 //  3)创建自定义颜色的SolidBrush 

//  4)画矩形,椭圆,扇形,多边形

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Drawing.Drawing2D;//另添加

 

namespacemyDrawBrushA

{

   publicpartialclass Form1 :Form

   {

       publicForm1()

       {

           InitializeComponent();

           SetStyle(ControlStyles.Opaque,true);//本例由于在OnPaint()中将客户区先涂上白色后再绘画,所以此句可有可无。

       }

 

       protectedoverridevoid OnPaint(PaintEventArgs e)

       {

           //base.OnPaint(e);

           Graphics g =e.Graphics;

           g.FillRectangle(Brushes.White, ClientRectangle);//全部客户区涂上白色

           g.FillRectangle(Brushes.Red,new Rectangle(20, 20, 80, 80));//红色正方形

 

           //以下创建一个Brush画渐变色正方形

           BrushlinearGradientBrush =newLinearGradientBrush(

               newRectangle(20, 110, 80,80), Color.Blue, Color.White,45);//创建渐变色brush,渐变方向:45-左上至右下;90-向下:......

           g.FillRectangle(linearGradientBrush,new Rectangle(20, 110, 80, 80));//画渐变色正方形

           linearGradientBrush.Dispose();//注意及时删除渐变色brush

 

           //以下用Brushes(不要创建Brush

           g.FillEllipse(Brushes.Aquamarine,new Rectangle(110,20, 100, 60));//椭圆(前两个参数是外接矩形左上角坐标)

           g.FillPie(Brushes.Chartreuse,new Rectangle(110, 110, 80, 80), 90,270);//扇形(207为圆心角度数,90为扇形旋转的角度

           g.FillPolygon(Brushes.BlueViolet,new Point[]{

               newPoint(220,10),

               newPoint(300,10),

               newPoint(250,40),

               newPoint(350,80),

               newPoint(230,100)

           });      //多边形

 

        //以下用创建自定义颜色的SolidBrush画圆

                 

           SolidBrushtrnsRedBrush =new SolidBrush(Color.FromArgb(120, 255, 0,0));

           SolidBrushtrnsGreenBrush =new SolidBrush(Color.FromArgb(120, 0, 255,0));

           SolidBrushtrnsBlueBrush =new SolidBrush(Color.FromArgb(120, 0, 0,255));

           // Base and height ofthe triangle that is used to position the

           // circles. Each vertexof the triangle is at the center of one ofthe

           // 3 circles. The baseis equal to the diameter of the circles.

           float triBase =50;

           float triHeight =(float)Math.Sqrt(3 * (triBase * triBase)/4);

           // Coordinates of firstcircle's bounding rectangle.

           float x1 =200;

           float y1 =100;

           // Fill 3 over-lappingcircles. Each circle is a different color.

           g.FillEllipse(trnsRedBrush, x1, y1, 2 * triHeight, 2 *triHeight);

           g.FillEllipse(trnsGreenBrush, x1 + triBase / 2, y1 +triHeight,

           2 * triHeight, 2 * triHeight);

           g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2 * triHeight, 2 *triHeight;   

    }

   }

}


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Drawing.Drawing2D;  
  6. using System.Drawing.Imaging;  
  7.   
  8. namespace Project  
  9. {  
  10.     class ImageOperation  
  11.     {  
  12.         /// <summary>  
  13.         ///  Resize图片   
  14.         /// </summary>  
  15.         /// <param name="bmp">原始Bitmap </param>  
  16.         /// <param name="newW">新的宽度</param>  
  17.         /// <param name="newH">新的高度</param>  
  18.         /// <param name="Mode">保留着,暂时未用</param>  
  19.         /// <returns>处理以后的图片</returns>  
  20.   
  21.         public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode)  
  22.         {  
  23.             try  
  24.             {  
  25.                 Bitmap b = new Bitmap(newW, newH);  
  26.                 Graphics g = Graphics.FromImage(b);  
  27.                 // 插值算法的质量   
  28.                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  29.                 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);  
  30.                 g.Dispose();  
  31.                 return b;  
  32.             }  
  33.             catch  
  34.             {  
  35.                 return null;  
  36.             }  
  37.         }  
  38.         /// <summary>  
  39.         /// 剪裁 -- 用GDI+   
  40.         /// </summary>  
  41.         /// <param name="b">原始Bitmap</param>  
  42.         /// <param name="StartX">开始坐标X</param>  
  43.         /// <param name="StartY">开始坐标Y</param>  
  44.         /// <param name="iWidth">宽度</param>  
  45.         /// <param name="iHeight">高度</param>  
  46.         /// <returns>剪裁后的Bitmap</returns>  
  47.         public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)  
  48.         {  
  49.             if (b == null)  
  50.             {  
  51.                 return null;  
  52.             }  
  53.             int w = b.Width;  
  54.             int h = b.Height;  
  55.             if (StartX >= w || StartY >= h)  
  56.             {  
  57.                 return null;  
  58.             }  
  59.             if (StartX + iWidth > w)  
  60.             {  
  61.                 iWidth = w - StartX;  
  62.             }  
  63.             if (StartY + iHeight > h)  
  64.             {  
  65.                 iHeight = h - StartY;  
  66.             }  
  67.             try  
  68.             {  
  69.                 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);  
  70.                 Graphics g = Graphics.FromImage(bmpOut);  
  71.                 g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);  
  72.                 g.Dispose();  
  73.                 return bmpOut;  
  74.             }  
  75.             catch  
  76.             {  
  77.                 return null;  
  78.             }  
  79.         }  
  80.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值