Solved

Display Image Files

Posted on 2004-04-26
10
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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