|
Load images from CImage array to chart |
Posted by Medic89 on May-18-2023 22:16 |
|
Hello Peter,
in my project I have a vector array (type CImage) with pictures, and I want to display them one after the other on my chart. But how to load those pictures to the Basechart? I used a loop and I treid to add ever item of my Array using the setResource() function with a unique ID and the memory adress, but the compiler doesn't like the "&Pictures1[i]" argument.
for (int i = 0;i < Pictures1.size();i++)
{
char resource_path2[256];
sprintf(resource_path2, "Add%d", i);
nextPage->setResource(resource_path2, &Pictures1[i]);
} |
Re: Load images from CImage array to chart |
Posted by Peter Kwan on May-19-2023 00:19 |
|
Hi Medic89,
The CImage is in hardware dependent format (it depends on the device context). This is useful if you want to sent the image directly to the hardware (to the screen device, printer device, etc). However, it is not useful if the image is for further manipulation, as the image format can be considered as unknown.
If the CImage is created from another image (PNG/JPG/BMP), please consider not to use the CImage, but use the original standard based image instead.
If you must use CImage, you need to convert the CImage back to a standard based image (such as PNG) using CImage::Save, then you can use the PNG images in your chart. See:
https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cimage-class?view=msvc-170#save
If the PNG images are files, then you can use the file path directly, and setResource is not necessary. If the PNG images are in memory, you can use nextPage->setResource(resource_path2, MemBlock(ptr, size)); to set the resource.
Best Regards
Peter Kwan |
Re: Load images from CImage array to chart |
Posted by Medic89 on May-20-2023 00:26 |
|
Hi Peter,
thanks a lot for your reply. I want to load jpg images to my App, I don't want just to save the paths, because until my chart uses the pictures, they might got deleted from the drive or something else...
What type would you use for storing the picutes? I tried to use the gdiplus Image type, but when calling setResource, I don't know the size of the item, which is required for the memblock term. |
Re: Load images from CImage array to chart |
Posted by Peter Kwan on May-20-2023 03:49 |
|
Hi Medic89,
The pictures can be stored in the memory as bytes, just like how everything is stored in memory. To specify where are bytes, you need to have a pointer to the first byte, and also how many bytes are there all together. When you get both of them, you can use them to create the MemBlock, which is just a structure to combine a pointer and the size into one variable.
Note that the bytes must store a standard image like JPG, PNG, BMP, etc.. The Image object is not an image. It is an object that contains an image and some other information. The image also is not in JPG, PNG, BMP, etc.. (If you use a JPG file to create the gdiplus Image, it will decode the JPG file into an opaque format that can only be used by gdiplus.)
If you do not want to use file path, you can always use MemBlock. If you must use CImage or Image, it allows you to save the image to an IStream, which can be a block of memory. It will provide both the pointer and the size of the memory. Note that in C++, your code is responsible for memory management, so you have to keep the memory available and only free the memory when it is no longer needed.
I am wondering how you got your JPG images in the first place. Are they from files, or are you using some sort of database query to obtain them as blob, or are they store as resources of the executable or some other method? If they are from files, opening and reading the files will read the bytes.
Regards
Peter Kwan |
Re: Load images from CImage array to chart |
Posted by Medic89 on May-21-2023 02:01 |
|
Hello Peter,
the images I want to load are photos from the hard drive. I want to load them at any time during runtime, and at the end the user is going to print a report, which is supposed to hold all the loaded pictures, thats why I don't want to work with resource paths, since the picture could have been deleted or renamed in the meantime. |
Re: Load images from CImage array to chart |
Posted by Peter Kwan on May-22-2023 16:12 |
|
Hi Medic89,
You may consider to use standard C functions to open the files and read them into memory.
std::vector<MemBlock> allFiles;
FILE *f = fopen("myFile.jpg", "rb"); // open file normally
fseek(f, 0, SEEK_END); // go to end of file to determine file size
long size = ftell(f);
char *data = malloc(size); // allocate memory to store the file content
rewind(f);
fread(data, size, 1, f); // read the whole file into memory
fclose(f);
allFiles.push_back(MemBlock(data, size));
Best Regards
Peter Kwan |
|