473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening picture file in C

Can anybody help me?

How to open picture file i.e.bmp of windows through C language?

Many Thanks,
Shahnaz

Mar 13 '06 #1
10 5090

Shahnaz wrote:
Can anybody help me?

How to open picture file i.e.bmp of windows through C language?


Get the filename into a string. Use `fopen` to open the file for
reading (do check whether it succeeded). Use `fread` to read out the
data. You may neet to `fseek`/`fgetpos`/`fsetpos` depending on the file
structure. Do not forget to close the file with `fclose`. Have a go,
and post your code if you have any problems (look up the functions
mentioned in a good C textbook or a manual).

If you're interested in the *format* of BMP files (or other image
formats), this is not the place to ask. Look for groups that deal with
graphics formats.

--
BR, Vladimir

Mar 13 '06 #2
Shahnaz wrote:
How to open picture file i.e.bmp of windows through C language?


Use `fopen`.

--
Chris "sparqling" Dollin
"Who do you serve, and who do you trust?"
Mar 13 '06 #3
Shahnaz wrote:
Can anybody help me?

How to open picture file i.e.bmp of windows through C language?

Many Thanks,
Shahnaz


#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
char cmd[256];

if (argc > 1) {
strcpy(cmd, "start ");
strcat(cmd, argv[1]);
system(cmd);
}
return 0;
}
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Mar 13 '06 #4
Hi Shahnaz,
First you have to learn more about bmp format,
about header ...
Second, this code example only open a picture and read first part of
header..
#include<stdio.h>

int main(void){

FILE *fp;
FILE *sai;
char ch;

int i,j,k;
unsigned short int tipo;

/*---------------- Cabe‡alho da Imagem----------------- */

struct imagem {
unsigned short int type;
unsigned int size;
unsigned short int reserved1,reserved2;
unsigned int offset;
} header;

/*-----------------Cabe‡alho de mapa de bits------------*/

struct Bmp2 {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} infoheader ;

struct imagem tmp;

/*--------------------- lendo cabeçalho imagem--------------*/
clrscr();

if ((fp=fopen("synapsis.bmp", "rb" ))==NULL){
printf("nÆo , possivel abrir o arquivo");
exit(1);
}

while (ch != EOF){
ch = getc(fp);
printf("%x",ch);
}

if (fread (&header, sizeof(struct imagem),1,fp) < 1){
printf("o arquivo nÆo pode ser lido");
fclose(fp);
exit(1);
}

fseek(&header,0,SEEK_SET);
printf("Lendo!!\n");
printf("%x\n",&header.type);
printf("%x %x\n",&header.reserved1,&header.reserved2);
printf("%x\n",&header.size);
printf("%x\n",&header.offset);

/*------------lendo mapas de bits da Imagem------------------*/
if (fread (&infoheader, sizeof(struct Bmp2),1, fp) < 1 ){
printf("o mapa de bits nÆo pode ser lido");
fclose(fp);
exit(1);
}
printf("\n");
fseek(&infoheader,0,SEEK_SET);
/*printf("%x\n", &infoheader.size);*/
}

Mar 14 '06 #5
"Romulo Carneiro" <ro***********@gmail.com> writes:
First you have to learn more about bmp format,
about header ...
The specific format is off-topic here.
Second, this code example only open a picture and read first part of
header..
#include<stdio.h>

int main(void){

FILE *fp;
FILE *sai;
char ch;

int i,j,k;
unsigned short int tipo;

/*---------------- Cabe‡alho da Imagem----------------- */

struct imagem {
unsigned short int type;
unsigned int size;
unsigned short int reserved1,reserved2;
unsigned int offset;
} header;

/*-----------------Cabe‡alho de mapa de bits------------*/

struct Bmp2 {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} infoheader ;

struct imagem tmp;

/*--------------------- lendo cabeçalho imagem--------------*/
clrscr();

if ((fp=fopen("synapsis.bmp", "rb" ))==NULL){
printf("nÆo , possivel abrir o arquivo");
exit(1);
}

while (ch != EOF){
ch = getc(fp);
printf("%x",ch);
}
getc() returns an int, but you're assigning the result to a char
object. See the comp.lang.c FAQ, <http://www.c-faq.com/>,
particularly question 12.1.
if (fread (&header, sizeof(struct imagem),1,fp) < 1){


You've just read the entire input file up to EOF (or tried to), and
printed each character in hexadecimal (though I don't know why). Now
you try to use fread() to read more data from the file.

Assuming you fix that, what makes you think that the types unsigned
short int and unsigned int in your particular C implementation match
the sizes of the corresponding fields in the input file? And how do
you know that the layout of "struct imagem" will match the layout of
the file? Hint: you don't.

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 14 '06 #6
yes thompson, you are right. I would like to show his how he can open
image file this example code . This code works but is only a short
template to begin understand. Thank for show me the details.

Mar 15 '06 #7
Answer you last question about types...this types are used in bitmap
image.

Mar 15 '06 #8
On Wednesday 15 March 2006 16:54, Romulo Carneiro opined (in
<11**********************@z34g2000cwc.googlegroups .com>):
Answer you last question about types...this types are used in bitmap
image.


Nobody knows what you're on about, since you didn't quote any context.

Read <http://cfaj.freeshell.org/google/>. This
one <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c> is also
useful.
--
BR, Vladimir

Victory uber allies!

Mar 15 '06 #9
"Romulo Carneiro" <ro***********@gmail.com> writes:
Answer you last question about types...this types are used in bitmap
image.


This doens't really answer the question.

BTW, what question. It's difficult to tell without context. Read
<http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 15 '06 #10
First of all *please* include context with your replies, here I restored
the context manually, by including the appropriate quotes and
attributions from the previous messages.
On 2006-03-15, Romulo Carneiro <ro***********@gmail.com> wrote:
"Keith Thompson" <ks***@mib.org> wrote:
Assuming you fix that, what makes you think that the types unsigned
short int and unsigned int in your particular C implementation match
the sizes of the corresponding fields in the input file? And how do
you know that the layout of "struct imagem" will match the layout of
the file? Hint: you don't.


Answer you last question about types...this types are used in bitmap
image.


No they are not. Any binary file format specifies fields in respect to
their size. You use an unsigned short
for some field of the bmp structure with the implicit assumption that it
would be 2 bytes. While this might be true on your particular
platform, that does not make it true in general. The C standard only
specifies that an unsigned short must have a USHRT_MAX >= 65535. So when
you take your code to a platform where unsigned shorts are 32bits long,
you are in trouble.

Also, by reading the whole struct at once with an fread() you assume
that the struct is layed out in memory in exactly the same way as it is
in the bmp file format. This of course is not a portable assumption as a
compiler is free to include padding bits between the elements of a
structure.

--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggraph.org
http://nuclear.demoscene.gr/
Mar 16 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3349
by: Hari Prasad | last post by:
Hi, I have downloaded some image files by connecting to a URL and I am trying open that image file using BrowserLauncher. I am using windows xp and the default one to open image files is windows...
4
13701
by: Shumit Rehman | last post by:
Hi I have a table which has path names to photos I would like to view. I would like to open some/any kind of image viewer(Paint) to see the picture when I click the pathname. The pictures are...
1
8964
by: Tim Marshall | last post by:
In my not too successful attempts to get an OLE chart object (Graph 11.0) that has been manipulated on a form to be reproduced on a report, I am considering the following procedure. First copy the...
13
2975
by: bytesc | last post by:
Hey guys, I have created many picture files using gd_library, I recently decided to rename all of my pictures files on my web server to text instead of just numbers. Example: productsssh.jpg ...
0
1607
by: bytesc | last post by:
Hey guys, I have created many picture files using gd_library, I recently decided to rename all of my pictures files on my web server to text instead of just numbers. Example: productsssh.jpg ...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.