Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

 

'I'm 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()

This doesn't take a snapshot of the control, but it tries to emulate it.

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

Posted

This should work for a multi-line textbox.

'I'm 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()

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

  • Leaders
Posted

you could do something like this :

   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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...