반응형
private void Button1_Click(object sender, EventArgs e) { DrawSplitText(this.pictureBox1.CreateGraphics() , this.textBox1.Text , new Font(this.textBox1.Font.FontFamily, 20, FontStyle.Bold) , new Rectangle(this.pictureBox1.Location, this.pictureBox1.Size) , new SolidBrush(Color.Black) , new SolidBrush(Color.White)); } // Draw split text centered in the indicated rectangle. private void DrawSplitText(Graphics gr, string text, Font font, Rectangle rect, Brush top_fg_brush, Brush bottom_fg_brush) { // Make bitmaps holding the text in different colors. Bitmap bm_top = new Bitmap(rect.Width, rect.Height); Bitmap bm_bottom = new Bitmap(rect.Width, rect.Height); // Make a StringFormat to center text. using (StringFormat sf = new StringFormat()) { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; using (Graphics gr_top = Graphics.FromImage(bm_top)) { gr_top.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; gr_top.FillRectangle(bottom_fg_brush, rect); gr_top.DrawString(text, font, top_fg_brush, rect, sf); } using (Graphics gr_bottom = Graphics.FromImage(bm_bottom)) { gr_bottom.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; gr_bottom.FillRectangle(top_fg_brush, rect); gr_bottom.DrawString(text, font, bottom_fg_brush, rect, sf); } } // Fill the top and bottom halves of the rectangle. RectangleF top_rect = new RectangleF( rect.X, rect.Y, rect.Width, rect.Height / 2f); using (TextureBrush brush = new TextureBrush(bm_top)) { gr.FillRectangle(brush, top_rect); } RectangleF bottom_rect = new RectangleF( rect.X, top_rect.Bottom, rect.Width, rect.Height / 2f); using (TextureBrush brush = new TextureBrush(bm_bottom)) { gr.FillRectangle(brush, bottom_rect); } bm_top.Dispose(); bm_bottom.Dispose(); }
'프로그래밍 > C#' 카테고리의 다른 글
[C#] 웹데이터 가져올때 한글깨짐[eur-kr] (0) | 2021.03.24 |
---|---|
[ C#] 생성자에서 생성자 호출하기 (0) | 2020.05.18 |
[C#]이미지 크기 변경 (0) | 2019.06.23 |
[C#]듀얼 스크린 Capture (0) | 2019.06.11 |
[C#]OpenCvSharp 이미지 로드 (0) | 2019.02.05 |