Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Display Image Files

Posted on 2004-04-26
10
Medium Priority
?
683 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

  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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
Suggested Courses

618 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