How to make an image that is transparent in the background?

 

Hello friends;

As I have tested before, we can just use images that are bmp type and also they should be 16 and 24 I think. I wanted to make an object that has no background. Let me explain with an example:

I used this article. The problem is when I want to create an image I don't know how to make transparent background. I used black bg and white bg, but none of them worked.
Do you know how I can fix it?

Graphical Interfaces XI: Integrating the Standard Graphics Library (build 16)
Graphical Interfaces XI: Integrating the Standard Graphics Library (build 16)
  • 2017.10.16
  • www.mql5.com
A new version of the graphics library for creating scientific charts (the CGraphic class) has been presented recently. This update of the developed library for creating graphical interfaces will introduce a version with a new control for creating charts. Now it is even easier to visualize data of different types.
 

The reason isn't the black vs white background — it's that a 24-bit BMP has no alpha channel at all, so there's nothing to make transparent. Two things usually fix this:

1. If you don't actually need an image file, skip the BMP and draw straight onto a canvas with an alpha-capable format. The default  CreateBitmapLabel  format is  COLOR_FORMAT_XRGB_NOALPHA  (opaque) — that's the usual culprit. Use  ARGB_NORMALIZE  and erase to a fully transparent background:

#include <Canvas\Canvas.mqh>
CCanvas canvas;
canvas.CreateBitmapLabel("myImg", x, y, w, h, COLOR_FORMAT_ARGB_NORMALIZE);
canvas.Erase(0);                                   // 0 = fully transparent
canvas.FillCircle(w/2, h/2, 40, ColorToARGB(clrDodgerBlue, 255));
canvas.Update();

Erase(0) leaves the background see-through and only what you draw (with alpha in the color's A byte) shows up.

2. If you specifically need to load a picture, it has to be a 32-bit BMP with a real alpha channel — and save it in top-down row order. CCanvas has a long-standing quirk where a bottom-up BMP renders only the top half, which sends a lot of people down the wrong path.

ARGB_NORMALIZE premultiplies alpha for you, so blending over the chart looks correct out of the box. Start there.

 
Mehdi Ghorbani Saeidian:

Hello friends;

As I have tested before, we can just use images that are bmp type and also they should be 16 and 24 I think. I wanted to make an object that has no background. Let me explain with an example:

I used this article. The problem is when I want to create an image I don't know how to make transparent background. I used black bg and white bg, but none of them worked.
Do you know how I can fix it?

I couldn't get transparency to work with BMP's either. I used the MQL5 PNG library to use PNG's instead.
PNG
PNG
  • 2023.07.15
  • www.mql5.com
Forget about BMP files like a bad dream. Thanks to this library, you can now use the PNG format, which has a number of advantages, such as being more compact without losing image quality and maintaining transparency.
 

The catch is the format: a 16- or 24-bit BMP has no alpha channel, so it simply can't carry transparency — there's no "no background" for it to store, which is why nothing you tried worked, whatever colour you used.

You need an image with an alpha channel:

  • a PNG — transparency built in, simplest path, and it's what Connor switched to, or
  • a 32-bit BMP with an alpha channel — save it top-down, or only the top half shows.

Make it in any editor (Paint.NET, GIMP, Photoshop) with the background erased to transparent, export as PNG or 32-bit BMP, and load that — the bars will sit with the chart showing around them, exactly like your example.

 
Krasimir Milchev #:

The catch is the format: a 16- or 24-bit BMP has no alpha channel, so it simply can't carry transparency — there's no "no background" for it to store, which is why nothing you tried worked, whatever colour you used.

You need an image with an alpha channel:

  • a PNG — transparency built in, simplest path, and it's what Connor switched to, or
  • a 32-bit BMP with an alpha channel — save it top-down, or only the top half shows.

Make it in any editor (Paint.NET, GIMP, Photoshop) with the background erased to transparent, export as PNG or 32-bit BMP, and load that — the bars will sit with the chart showing around them, exactly like your example.

I've tried unsuccessfully with Krita, Photoshop CS6, and MS Paint. MS Paint was an old version, perhaps newer versions will work better. One thing I like about the PNG library is that it has a resize function so I can scale the PNG for high-resolution displays.