-1

See below example - Ive tried everything (incl. editing image and changing bg color from transparent to white), but still image background is being displayed as gray.

Does anyone know how to fix that ?

Example

-- EDIT--

Here is my code:

Clipboard.SetImage(Properties.Resources.smile);
richTextBox1.Paste();

I can see other people have similar issue: Image background after insert into RichTextBox

Ive also tried solution mentioned there

Maciej
  • 10,423
  • 17
  • 64
  • 97
  • 1
    `Ive tried everything` You know you have to show us that code that you tried. – LarsTech Apr 03 '19 at 22:59
  • 1
    You could: transform the Bitmap to a WMF metafile; Get the Image from the ClipBoard, paint it yourself using the RichTextBox background color as the Bitmap canvas color and re-paste it; Use the WPF RichTextBox. – Jimi Apr 04 '19 at 00:56
  • I forgot to mention that if you get the Bitmap directly, the Image transparency is lost. You need to get the `DataFormats.Dib`. Which, of course, is not directly supported by the .Net graphics class (as the native WMF format). – Jimi Apr 04 '19 at 01:07
  • 1
    _incl. editing image and changing bg color from transparent to white_ This __can't be right! There is no concept__ of a 'background' in a bitmap. Pixels are pixels and white ones will be white. So you are __doing something else__ that is wrong! – TaW Apr 04 '19 at 07:38
  • Ive added more info – Maciej Apr 04 '19 at 19:36

2 Answers2

1

The problem is not your image, it's the fact that richTextBox can't handle transparency in the image.So you should consider using another control Or make background same color as richTextBox, hope it helps

Ehsan Kiani
  • 3,050
  • 1
  • 17
  • 19
0

I've just resolved that by converting *.png to *.jpg (bmp and tiff also worked)

Many thanks everyone tried to help. I think issue is with image transparent background, that's why conversion helped. In my case I didnt require transparency, just wanted avoid to see frame around

EDIT

Ive found solution here: https://stackoverflow.com/a/27318979/77273

(replace transparent color by specified)

My new code now is as follow:

Clipboard.SetImage( Transparent2Color (Properties.Resources.angry, Color.White)  );
richTextBox1.Paste();

EDIT-2

Ive just discovered why gray background was shown even if Ive edited picture and replaced transparent background on white.... (This led me to wrong direction in fact)

  • Ive just replaced img in resources, using same name
  • turned out that VS somehow 'cache' old version of picture
  • it started working when Ive used other name or removed file from resources and copied it again

Hope this helps someone who has experiencing similar issues.

Maciej
  • 10,423
  • 17
  • 64
  • 97