0

I've been trying this for a while with transparent textBox, but it seems that it's completely bugged so I have to ask it here. How do I use this pictureBox to write text on it (use pictureBox as textBox):

PictureBox

Thanks a lot for any answer you'll give me.

Purixi
  • 139
  • 1
  • 1
  • 8
  • 1
    If you want to put text over PictureBox, why not to use Label instead of TextBox? – Paviel Kraskoŭski Feb 07 '16 at 11:50
  • @Purixi You can use [solution described here](http://stackoverflow.com/questions/16050249/transparency-for-windows-forms-textbox) to implement `TextBox` with transparent background. – tchelidze Feb 07 '16 at 11:53
  • @tchelidze I tried it already and it works really shitty. – Purixi Feb 07 '16 at 11:59
  • You're always going to struggle in making a graphical interface in winforms, the only *real* solutions are to either have a less graphical interface or to switch to wpf which doesn't have these problems (with transparency) – Sayse Feb 07 '16 at 12:04
  • @Sayse Switching to WPF is not a variant, honestly. Have to figure out how to make it work in WinForms. – Purixi Feb 07 '16 at 12:10
  • 3
    Hmm. The real question here is ***why***? Why do you want to do this? What problem are you trying to solve? Why would you ever use a PictureBox when your goal is to display text? – Cody Gray - on strike Feb 07 '16 at 13:07

2 Answers2

1

Use a Panel instead of a Picturebox. Draw on the Panel. Put a Textbox on the Panel. You'll find that the panel will save as an image (bmp,jpg etc) including the Textbox.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 04 '23 at 00:33
0

The whole way winforms does transparency is a hack, and what you're trying to do isn't really going to be possible.

It does this by taking the background color of the layer below it and sets it to its own color. You can see this in your own image as the background color of the picture box is the default control color, which in turn means that the labels color is this.

In the image you've shown, the image seems to be a solid color, so it might be an option to set the text's background color to the same as that.

If its a dynamic image then you'll have to hack your own solution together, some that come to mind are:

  • Work out the color of the image at the labels position and then set it to the same (and hope its a solid color)
  • Have a panel around the controls so it at least looks presentable, of course you'd see less image this way
  • A picture box is essentially just a graphics canvas so you could just draw a string on it, but this is still a hack.
Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 2
    The claims at the top of your answer are misleading and not quite true. First off, large parts of WinForms are based around GDI (not GDI+), especially the controls since they are just wrappers around native Win32 controls, which use GDI. Second, I *assume* what you mean by this claim that "all of the graphics processing...is done on the processor, and not the graphics card" is that GDI+ is not hardware-accelerated. Although that is true, I'm not really sure why that is relevant. Lack of hardware acceleration has nothing to do with a lack of transparency. Besides, GDI *is* hardware accelerated. – Cody Gray - on strike Feb 07 '16 at 13:10
  • @CodyGray - i've edited out that part of my answer, I thought it was GDI but second guessed myself. – Sayse Feb 07 '16 at 15:49
  • 2
    Better. Yeah, the issue is just that WinForms is a fairly simple wrapper around the native Win32 controls and since they were written in the late 1980s, there was little reason to support true transparency. (And I'd argue little reason for it now, but I guess I'm old-fashioned.) That's not the same thing as it being a hardware or rendering limitation, though. Layered windows have been supported by hardware and the API for 10+ years. If you wanted, you could write the code to make things transparent, you'd just have to do all of the drawing yourself and wouldn't get any help from the framework. – Cody Gray - on strike Feb 07 '16 at 15:52