Solved

Display Image Files

Posted on 2004-04-26
10
676 Views
Last Modified: 2008-02-01
Hi, I am just wondering which windows libraries I can use to display RGB colors in a form?
I built my windows form in VC++.net.

I think it should be really easy maybe just a two for loop to display it.. but i am not so sure about the library and
the functions to use..

Any help would be great, thanks
0
Comment
Question by:lordiano
  • 5
  • 4
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10918033
Managed or unmanaged code?
0
 

Author Comment

by:lordiano
ID: 10918132
Managed Form
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10918157
What exactly you want to do: show some image file in the form, draw rectangles, lines etc ?
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:lordiano
ID: 10918195
I want to show a .ppm image file on a form, I am not sure if it has to be managed or unmanage.. but right now my MainForm is created as Managed.
The .ppm file basically contains RGB (first byte R, 2nd G, 3rd B)
I dont know if there is already a windows API that i can call to display this, or i have to basically put a pixel by pixel on a form to display it?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10918274
What is ppm file - just raw file which doesn't contain any header information, only pixels? Or this is some image standard? If this is raw file, you need to know image dimensions, since file doesn't contain them.
To load raw file, read in using stream into memory. Create Bitmap of required size and type. After this you can copy pixels directly to bitmap, getting bitmap bits pointer using LockBits function.
Another way is using CreateDIBSection and SetBitmapBits. Bitmap object can be created from HBITMAP.

The last step, when you have Bitmap object, is setting it as Image property to PictureBox.
0
 

Author Comment

by:lordiano
ID: 10918351
It is some image standard, it will have header information about the size of the image ...etc

I was doing search on experts-exchange on relative questions, hmm could you maybe give me like a couple lines of syntax on copying the pixels to the HBITMAP object?
I can figure out the rest myself.. hopefully :P
0
 
LVL 17

Expert Comment

by:mokule
ID: 10918911
This may interest You.
HOWTO: Platform Independent Alpha Blending.
There are operations on bitmaps perfomed there.

http://support.microsoft.com/default.aspx?scid=kb;en-us;303916
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10927610
This format is not supported by .NET, so you need to do this yourself or found some library that does this.
Assuming that you know how to extract image header and data from such file, here is simple (but slow) way to show this file:
1) Create Bitmap instance using constructor:

public: Bitmap(
   int width,
   int height,
   PixelFormat format
);

Use Format24bppRgb format and image size known from the header

2) Read all image pixels from the file and set them to bitmap using Bitmap method:

public: void SetPixel(
   int x,
   int y,
   Color color
);

(see also Color::FromArgb method)

3) Set this bitmap as Image property of PictureBox control on the form.
0
 

Author Comment

by:lordiano
ID: 10927724
Hi Alex, this is what I have done :
void read_file(char * filename)
            {
                  int width, height, maxColorVal, R, G, B;
                  unsigned char ch;
                  char buf[100];
                  System::Drawing::Color mycolor;
                  ifstream infile("Dune.ppm", ios_base::in | ios_base::binary);
                  infile.getline(buf, 100);
                  if(infile.peek() == '#'){
                        infile.getline(buf,100);
                  }
                  infile>>width;
                  infile>>height;
                  infile>>maxColorVal;
                  Bitmap *mybmp = new Bitmap(width, height, System::Drawing::Imaging::PixelFormat::Format24bppRgb);

                  for(int i = 0; i < width; i++){
                        for(int j = 0; j < height; j++){
                              infile>>ch;
                              R = ch;
                              infile>>ch;
                              G = ch;
                              infile>>ch;
                              B = ch;
                              mycolor = System::Drawing::Color::FromArgb(R,G,B);
                              mybmp->SetPixel(i, j, mycolor);
                        }
                  }
            }
====================================================================
I am not so sure how to set the bitmap as image property.. i checked MSDN but Does not have a clue.. please help ! thanks a lot Alex
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10927794
Now add PictureBox control to the form. Suppose it is called pictureBox1:

pictureBox1.Image = mybmp;

By the way, you are using unmanaged code (ifstream) in managed application. This is OK, but you must understand what happens in your code. .NET way to to the same is using of FileStream and BinaryReader classes.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question