?
Solved

Display Image Files

Posted on 2004-04-26
10
Medium Priority
?
681 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
Technology Partners: 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 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