'graphic'에 해당되는 글 1건

  1. 2007.12.13 GDI+ 연습 - Graphics 객체를 얻기 위한 여러가지 방법
Study - Programming/C#2007. 12. 13. 11:26

using System;
using System.Windows.Forms;
using System.Drawing;


class Program : Form
{
    Button btn = null;
    ListBox lstbox = null;
    Image image = null;
    public Program()
    {
        this.Text = "Graphics 개체 얻기";
        btn = new Button();
        btn.Text = "버튼 위에 GDI+ 출력";
        btn.SetBounds(10, 10, 200, 100);
        btn.Click += new EventHandler(btn_Click);
        this.Controls.Add(btn);

        lstbox = new ListBox();
        lstbox.SetBounds(210, 110, 410, 310);
        lstbox.Items.Add("사과");
        lstbox.Items.Add("포도");
        lstbox.Items.Add("수박");
        lstbox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(lstbox_DrawItem);
        lstbox.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(lstbox_MeasureItem);

        this.Load += new EventHandler(On_Load);
        this.Controls.Add(lstbox);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics grfx = e.Graphics;
        if (image != null)
            grfx.DrawImage(image, 0, 0);
    }

    static void Main(string[] args)
    {
        Application.Run(new Program());
    }

    public void btn_Click(object sender, EventArgs e)
    {
        Graphics grfx = btn.CreateGraphics();
        grfx.FillRectangle(new SolidBrush(Color.Blue), btn.ClientRectangle);
        grfx.Dispose();

        Image imageFile = Image.FromFile("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\그림 샘플\\겨울.jpg");
        grfx =  Graphics.FromImage(imageFile);

        Font font = new Font("돋음",20);
        Brush brush = Brushes.Pink;

        grfx.DrawString("이미지에 글자 쓰기", font, brush, 10, 10);
        grfx.Dispose();

        imageFile.Save("sample.gif");
        this.image = Image.FromFile("sample.gif");
        this.Invalidate(this.ClientRectangle);
  
    }

    private void On_Load(object sender, EventArgs e)
    {
        lstbox.DrawMode = DrawMode.OwnerDrawFixed;
    }

    private void lstbox_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        Graphics g = e.Graphics;
        Console.WriteLine("{0} : MeasureItem 이벤트 실행", e.ToString());
    }

    private void lstbox_DrawItem(object sender, DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        Brush brush = Brushes.Black;

        switch(e.Index)
        {
            case 0:
                brush = Brushes.Red;
                break;
            case 1:
                brush = Brushes.Violet;
                break;
                case 2:
                brush = Brushes.Green;
                break;
        }

        g.DrawString(lstbox.Items[e.Index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);

        Console.WriteLine("{0} : DrawItem 이벤트 실행", e.ToString());
    }
}

소스가 좀 많다. 이외에도 몇가지가 있는데, 한 소스에 담기가 좀 번거로워서 한번에 담을 수 있는것만 해 놓았다.

주석이 없어서 소스 분석이 좀 어렵겠다. 주석 습관 들여야 하는데..

사용한 방법은 다음과 같다.

1. Control Class의 CreateGraphics메서드 이용
2. ListBox 등의 컨트롤에서 제공하는 MeasureItem이나 DrawItem 이벤트 등을 이용한 방법
3. Graphics.FromImage() 메서드 이용
4. OnPaint()메서드 override 해서 사용


이외에도 Paint이벤트 상속받기, PrintPage이벤트 핸들러,Win33API 사용하기 등의 방법이 있다.

Posted by 머리