■ Bitmap 클래스를 사용해 투명 배경 비트맵을 구하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.pictureBox.Image = GetTransparentBitmap(this.pictureBox.Width - 10, this.pictureBox.Height - 10); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 투명 비트맵 구하기 - GetTransparentBitmap(width, height) /// <summary> /// 투명 비트맵 구하기 /// </summary> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <returns></returns> private Bitmap GetTransparentBitmap(int width, int height) { Bitmap bitmap = new Bitmap(width, height); using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.Transparent); using(Pen pen = new Pen(Color.RoyalBlue, 20)) { int ellipseWidth = width - (int)pen.Width; int ellipseHeight = height - (int)pen.Width; ellipseWidth = ellipseWidth < 0 ? 1 : ellipseWidth; ellipseHeight = ellipseHeight < 0 ? 1 : ellipseHeight; int x = (width - ellipseWidth ) / 2; int y = (height - ellipseHeight) / 2; graphics.DrawEllipse(pen, x, y, ellipseWidth, ellipseHeight); } } return bitmap; } #endregion } } |