달력

52024  이전 다음

  • 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
반응형

http://csharphelper.com/blog/2019/07/draw-text-with-colors-reversed-in-its-upper-and-lower-halves-in-c/

 

 

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();
        }
Posted by 유령회사
|