Solved

Display Image Files

Posted on 2004-04-26
10
674 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now