天天看點

C# 繪圖三種方式

如果你想在窗體上進行繪畫。通常會使用以下方法.
方法1,利用控件或窗體的paint事件中的painEventArgs
private void form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;//建立畫闆,
}
方法2直接重載控件或窗體的OnPaint方法
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
}
方法3調用某控件的CreateGraphics方法
Graphics g = this.CreateGraphics();

方法4 調用Graphics類的FromImage靜态方法
在需要更改已存在的圖像時,通常會使用此方法
Image img = Image.FromFile("g1.jpg");//建立Image對象
Graphics g = Graphics.FromImage(img);//建立Graphics對象      
public Bitmap Create(int[] arry)
        {
            //獲得數組中最大值
            int max = 0;
            for (int i = 0; i < arry.Length; i++)
            {
                if (arry[i] > max)
                    max = arry[i];
            }
            Bitmap bitmap = new Bitmap(arry.Length, max+10);
            Graphics g = Graphics.FromImage(bitmap);//建立Graphics對象
            g.Clear(Color.White);
            Pen curPen = new Pen(Brushes.Black, 1);

            // g.DrawLine(curPen, 10, 0, 10, 30); //劃線 ; 水準坐标形同 10,0,10,30;  y坐标不同

            for (int i = 0; i < arry.Length; i++)
            {
                g.DrawLine(curPen, i, arry[i], i, 0); //劃線 ; 水準坐标形同 10,0,10,30;  y坐标不同
            }

            return bitmap;
        }      

轉載于:https://www.cnblogs.com/enych/p/10544592.html

c#