Tuesday, April 29, 2014

How To convert :TextBox value To Image C#


        public Color FontColor { get; set; }
        public Color FontBackColor { get; set; }
        public string FontName { get; set; }
        public string ImagePath { get; set; }
        public int FontSize { get; set; }
        public int ImageHeight { get; set; }
        public int ImageWidth { get; set; }
        public Bitmap ImageText { get; set; }
        public Graphics ImageGraphics { get; set; }
        public Font ImageFont { get; set; }
        public SolidBrush BrushForeColor { get; set; }
        public SolidBrush BrushBackColor { get; set; }
        public PointF ImagePointF { get; set; }

        private void DrawText(string text)
        {
            FontColor = Color.Red;
            FontBackColor = Color.Yellow;
            FontName = "Arial";
            FontSize = 10;
            ImageHeight = textBox1.Height;
            ImageWidth = textBox1.Width;
                        //saveimage in bin of the project with textbox name content
            ImagePath = Application.StartupPath + "\\" + textBox1.Text + ".JPEG";

            //this for save image in ecery place you need ..Name of image ==textbox value
            // ImagePath =  @"C:\"+textBox1 .Text +".JPEG";

            ImageText = new Bitmap(ImageWidth, ImageHeight);

            ImageGraphics = Graphics.FromImage(ImageText);

            ImageFont = new Font(FontName, FontSize);
            ImagePointF = new PointF(5, 5);
            BrushForeColor = new SolidBrush(FontColor);
            BrushBackColor = new SolidBrush(FontBackColor);

            ImageGraphics.FillRectangle(BrushBackColor, 0, 0, ImageWidth, ImageHeight);
            ImageGraphics.DrawString(text, ImageFont, BrushForeColor, ImagePointF);

            ImageText.Save(ImagePath, ImageFormat.Jpeg);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DrawText(textBox1.Text);
        }


No comments:

Post a Comment