Sunday, 9 December 2018

How to insert images in C/ C++ using graphics(100% working)

Hello friends today we will learn how to insert images in  C++ language very easily using graphics.The things that are required for this work are listed here .....

1.Codeblocks compiler installed on your system:

Code::Blocks is a freeopen-source cross-platform IDE that supports multiple compilers including GCCClang and Visual C++. It is developed in C++ using wxWidgets as the GUI toolkit. Using a plugin architecture, its capabilities and features are defined by the provided plugins. Currently, Code::Blocks is oriented towards CC++, and Fortran. It has a custom build systemand optional Make support.(by wikipedia).

2.Graphics libraries should set up with codeblocks:

When we install codeblocks then there is no library and header file of graphics are given by the developers with it, so we have to configure these things manually.I come across a lots of videos on youtube to configure graphics in codeblocks and i found all the videos are just waste of time except one video and i learnt  from that video .It was a bit difficult for me to configure graphics in codeblocks.
This is why because i was getting the libraries and header file with bug.
So i thought to create a video on this topic on my own  :How to use graphics in codeblocks video






  If you did these two things then you can use this program or concept to insert images in C++ language using graphics in Codeblocks.
Here we go:
 #include<iostream>  
 #include<graphics.h>  
 int main()  
 {  
   initwindow(600,400,"Images");  
   readimagefile("image_name.jpg",0,0,600,400);  
   getch();  
   readimagefile("image_name.jpg",0,0,600,400);  
   getch();  
   return 0;  
 }  



Description of the program:


If you are using codeblocks to make programs in graphics mode then we can make the programs in only C++ because in the graphics.h header file there is reference to iostream and if we work in C language then it will not link and then we get error in header file graphics.h.



here we have used the two important predefinded functions of graphics they are....

  • initwindow():this function is used to initialise a new window or you can say a  new window
  • readimagefile():this is the function that is used to set images in our program.
readimagefile() takes five parameters the first parameter is the name of the image with extension of the image and the rest are the co-ordinates of the screen where to set.


Thanks for visiting ...we will come with new typical programs so stay connected.







How to insert image in c language in turboc IDE

Its been very interesting as well as difficult to work with graphics in c or c++ languages.
But it will be more interesting to work with image insertion in C or C++ language.

I have already created the post to insert image in c/c++ in codeblocks IDE but now its time to achieve this using Turbo C++ IDE.

 Befor writing code we have to take care of  these points.
  • Create a  bmp(16 color Bitmap) image file using paint or any other image editor. 
  • paste that file into bin folder of turbo c where this program will be saved.
  • After pasting we will continue to write the below code.




 
 #include <alloc.h>  
   #include <conio.h>  
   #include <graphics.h>  
   #include <stdio.h>  
   #include <stdlib.h>  
   struct BMP  
   {  
    char Type[2];      //File type. Set to "BM".  
    unsigned long Size;   //Size in BYTES of the file.  
    unsigned long Reserved;   //Reserved. Set to zero.  
    unsigned long OffSet;  //Offset to the data.  
    unsigned long headsize; //Size of rest of header. Set to 40.  
    unsigned long Width;   //Width of bitmap in pixels.  
    unsigned long Height;   // Height of bitmap in pixels.  
    unsigned int Planes;  //Number of Planes. Set to 1.  
    unsigned int BitsPerPixel;    //Number of Bits per pixels.  
    unsigned long Compression;  //Compression. Usually set to 0.  
    unsigned long SizeImage; //Size in bytes of the bitmap.  
    unsigned long XPixelsPreMeter;   //Horizontal pixels per meter.  
    unsigned long YPixelsPreMeter;   //Vertical pixels per meter.  
    unsigned long ColorsUsed;  //Number of colors used.  
    unsigned long ColorsImportant; //Number of "important" colors.  
   };  
   int ShowBMP(int x, int y, char* FileName)  
   {  
     int b,a;  
     struct BMP Obj;  
     unsigned char* Datas;  
     int in=0;  
     unsigned char c=0;  
     FILE * fp;  
     fp = fopen(image.bmp,"rb"); //give filename of image with extension.  
     if(!fp){  
     printf("Error : Unable to open file ..");  
     exit(0);  
     }  
     fread(&Obj, sizeof(Obj), 1, fp);  
     if(Obj.BitsPerPixel!=4) // This isn't a 16 color bmp we can read;  
     {  
      fclose(fp);  
      printf("Error : File format not supported ..");  
      exit(0);  
     };  
     fseek(fp,Obj.OffSet,SEEK_SET);  
     Datas=(unsigned char*) calloc(Obj.Width/2+1, sizeof(unsigned char));  
     for(b=Obj.Height;b>=0;b--)  
     {  
      fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);  
      c=0;  
      in=0;  
      for(a=0;a<=Obj.Width;a+=2)  
      {  
        c = (Datas[in] | 0x00) >>4;  
        putpixel(a+x,b+y,c);  
        c = (Datas[in] | 0xF0) & 0x0F;  
         putpixel(a+1+x,b+y,c);  
        in++;  
      }  
     }  
     free (Datas);  
     fclose(fp);  
     return 1;  
   }  
   void main()  
   {  
   int color;  
   int gd , gm ;  
   gd = VGA ; gm = VGAHI;  
   initgraph(&gd,&gm,"");  
   ShowBMP(0,0,"j.bmp"); /* Enter File Name Here */  
   getch();  
   closegraph();  
   }  

Now Run the code and you will get your image as output.
 
 

WhatsApp will give Rs 1.8 crores to new Indian startups, Here is all you need to know

The "Startup India WhatsApp Grand Challenge" has been appraise growth in the entrepreneurial sector. Total prize money of Rs 1.8...