develop

c# 도형 그리기. Graphics, Drawing, Bitmap

파드파드 2023. 11. 13. 09:50
반응형

요점 1. Graphics 에 Bitmap을 참조 시켜서 직접 그리는 방식은 저장을 하기 위해서는 필요 함.

단, 리사이즈 및 이동 시에 그려놓은 이미지가 위치 이동이 될 수 있음

 

요점 2. 그래서 pictureBox에 직접 paint 이벤트에서 Drawing을 하면 리사이즈 및 이동시에도 위치 변경이 없음.

단, 저장을 위해서는 Bitmap에 그리는 행위가 또 필요함.

 
 public Bitmap GenerateBitmap(PictureBox target)
    {
        int wt = target.ClientSize.Width, ht = target.ClientSize.Height;
        Bitmap bmp = new Bitmap(wt, ht, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bmp);
        Render(g, target);
        return bmp;
    }

    pictureBox1.Paint += (s, ev) =>
        {
            ev.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            ev.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            grid.Render(ev.Graphics, pictureBox1);
        };
        pictureBox1.Resize += (s, ev) =>
        {
            pictureBox1.Invalidate();
        };

 

 

출처

https://stackoverflow.com/questions/70845452/c-sharp-bitmap-drawing-using-graphics

반응형