Graphics to Image

clearz

Member
Joined
Jul 21, 2003
Messages
22
Hi

TextBox t = new TextBox();
t.Text = "Hello";
Graphics g = t.CreateGraphics();

Ok this gets the graphics object of a textbox. What I would like to do is create an image object from that textbox am I on the right track using a Graphics object or is there some other way. Any help would be appricated.

Thanks
John
 
The code you posted allows you to draw on the textbox, not create an image from that textbox. Heres something more along the lines of what you want (I think):

[VB]
Im assuming TextBox1 has been created design time
Dim bmp as new Bitmap(Textbox1.Width, TextBox1.Height)
Dim G as Graphics = Graphics.FromImage(bmp)
Dim txtRect as New Rectangle(0, 0, TextBox1.Width, TextBox1.Height)
G.FillRectangle(TextBox1.BackColor, txtRect)
G.DrawRectangle(Pens.Black, txtrect)
G.DrawString(TextBox1.Text, Brushes.Black, 2, 2)
G.Dispose()
[/VB]
This doesnt take a snapshot of the control, but it tries to emulate it.
 
This should work for a multi-line textbox.
[VB]
Im assuming TextBox1 has been created design time
Dim bmp As New Bitmap(Textbox1.Width, TextBox1.Height)
Dim G As Graphics = Graphics.FromImage(bmp)
Dim txtRectF As New RectangleF(0, 0, TextBox1.Width, TextBox1.Height)
G.FillRectangle(TextBox1.BackColor, txtRect)
G.DrawRectangle(Pens.Black, 0, 0, TextBox1.Width, TextBox1.Height)
G.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, txtRectF)
G.Dispose()
[/VB]
 
you could do something like this :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.BackColor = TextBox1.BackColor
        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
        Application.DoEvents()
        PictureBox1.CreateGraphics.DrawString(TextBox1.Text, TextBox1.Font, New SolidBrush(TextBox1.ForeColor), 0, 0)
    End Sub
 
Hey clearz!
If you also want to draw a single/3D Border, take a look at the System.Windows.Forms.ControlPaint-Class! It provides methods to draw Borders and many other things that look exactly as the windows-Controls...

Andreas
 
Back
Top