Create a BITMAPINFO from a JPG/PNG file in Windows C++ - c++

I have a function in an API I am using that request a BITMAPINFO pointer. I am suppose to give this function an image. My images are JPG/PNGs. Suppose I transform them to BMP files, I have still no clue on how to set up this struct. Is there a windows API that I can use for it?

You can use GDI+ for this.
Read BMP/JPG/PNG/GIF using Gdiplus::Bitmap::FromFile to load and Gdiplus::Bitmap::GetHBITMAP to access HBITMAP and then BITMAPINFO

A quick google turns up this MSDN article: Sizing a JPEG or PNG Image which show one way of filling a BITMAPINFO for a JPEG file (would work for PNG).
Use the links on the left of that page to get more information about the bitmap API.

PNGs have, potentially, AlphaChannel/transparency information -- which gets lost when converting to a bitmap. What does your function that requires BITMAPINFO actually do ?
If your approach is to transform the files to bitmaps; don't do that at runtime.
Convert the files using a paint program to bmp.
As previously mentioned, you can use GDI+ to make use of several different native formats;
but doing so will likely require converting your existing GDI calls to GDI+.

Related

Is there any way to load a .png from my resources without using GDI+?

I am trying to load a .png I stored in my projects resources, in order to set it in a picture control, but I can't quite figure out why. I did some research, and it seems like .png is not really supported with the usual LoadImage()-call.
However, I don't really want to convert it to a bitmap if I can get around it.
So far, I only found resources on how to do it with GDI+, or really ancient win32-api code.
Is there any way to load .png files natively by todays standard?
The "new" way to do it would be Direct2D and WIC, which is demonstrated in the Windows Imaging Component and Direct2D Image Viewer Win32 Sample.
But if the rest of your application is basic controls, Direct2D is overkill. The image has to be converted to a bitmap at some point to be displayed – whether in your GPU or in memory – and GDI+ fits this use case.
If these resources are icons or some other small-ish file (<2mp), I would reccomend embedding the resource as a bitmap. You can keep your asset pipeline as PNG, just add a pre-build step to convert your PNGs to pre-multiplied BGRA bitmaps and use LoadResource. There are pre-built tools to meet this need.

How to create and edit a bitmap

I'm trying to create a bitmap that I can change the pixels of and then update the window with that bitmap. I've tried to research a way to just create a bitmap, but that was to no avail.
I need to know how to create a blank bitmap, then change its pixels using x and y coordinates and a color, and then how to draw it to the window. I don't need to save it to the computer and I don't want to convert an existing image into a bitmap.
I'm on windows and using Visual Studio with C++
Just to specify, I need to know the syntax for creating a blank bitmap with a certain width and height, and also what function I need to use to change the pixels' colors, and then how to draw a bitmap to the window. Thank you.
There is a free library ImgSource that you can use which has a huge amount of functionality for images.
http://www.smalleranimals.com/isource.htm
It is available in MSDN, but using a library like ImgSource may be to your advantage.
It provides simple methods for rendering your image onto a device context. And there is a good support forum.

How to get an HBITMAP of one image from an array of images from a resource?

I have a resource that has an id ID_IMAGES which is a PNG file. This resource is an array of images, all the same size (16x16). I'm trying to figure out if it is possible to pull out any one of these images and generate an HBITMAP from it.
I've looked around on the net, but I cannot find any info. Perhaps I'm using the wrong keywords?
Have a look at CImage class. Its Load() method can load a stream of data. CImage supports PNG format. Basically, you load the image from your resource and then use its Detach() method to get your HBITMAP

Loading PNG file. Need to get RGBA HBITMAP

I'm trying to create a splash screen that will show transparent png background. So at first I need to load png file into format that is acceptable for the UpdateLayeredWindow calls (basically HBITMAP with RGBA format).
At the moment, I've found Windows Imaging Component, but that does not work for VS2005 (which is mandatory requirement). Using some additional libraries is also a problem.
I've heard that GDI+ can do the trick, but I can't find a proper example unfortunately :( Can anyone help?

C++: What's the simplest way to read and write BMP files using C++ on Windows?

I would like to load a BMP file, do some operations on it in memory, and output a new BMP file using C++ on Windows (Win32 native). I am aware of ImageMagick and it's C++ binding Magick++, but I think it's an overkill for this project since I am currently not interested in other file formats or platforms.
What would be the simplest way in terms of code setup to read and write BMP files? The answer may be "just use Magick++, it's the simplest."
Related Question: What is the best image manipulation library?
When developing just for Windows I usually just use the ATL CImage class
EasyBMP if you want just bmp support. I'ts simple enough to start using within minutes, and it's multiplatform if you would need that.
A BMP file consists of 3 structures. A BITMAPFILEHEADER followed by a BITMAPINFO followed by an array of bytes.
The absolute simplest way to load a BMP file using Win32 is to call CreateFile, GetFileSize, ReadFile and CloseHandle to load the file image into memory, and then just cast a pointer to the buffer to a BITMAPFILEHEADER and go from there.
I lie, a simpler way is to call LoadImage. Making sure to pass the LR_DIBSECTION flag to ensure that GDI doesnt convert the loaded image into whatever bitdepth your primary display is configured to. This has the advantage of getting you a HBITMAP that you can select into a DC and therefore draw all over using GDI.
To save it however, there is no shortcut. You need to prepare a BITMAPFILEHEADER, write it out, fill in a BITMAPINFO struct, write that out, and then the actual pixel data.
I tried CImage as above, but I had a C array full of pixel values that I simply wanted to dump as a BMP (or any other format). CImage has no constructor for that, and I did not want to link MFC (for CBitmap) nor try to fathom IWIC.
What was easy was CImg:
#include <cimg/cimg.h>
using namespace cimg_library;
//...
void SaveMyData(uint8_t *pxarray, int width, int height)
{
CImg<uint8_t> img(pxarray, width, height);
img.save_bmp("sav.bmp");
}
The CBitmap class does BMP I/O.
#include <windows.h>
LoadImage
I've not used Magick++, but Windows has a library called the Windows Imaging Component which would likely suit your needs.