A talk on C language (ii) image display windows and Linux

Source: Internet
Author: User

There are many libraries available for image display, Windows has GDI,GDI+,D3D and so on, Linux has X window and wayland, in addition to opengl, SDL and other graphics libraries and various GUI Libraries.

Understanding the most primitive way is still helpful for deepening your understanding. The following is the simplest example of displaying bitmaps under Windows and Linux:

Examples of Windows displaying images with Gdi:

1 /*2 * FILENAME:IMAGE_WIN.C3 * Usage:tcc-luser32-lgdi32-run image_win.c4 */5 6#include <windows.h>7#include <stdlib.h>8 //9typedef unsignedChar byte;Ten  onetypedefstruct { a     intWidth; -     intHeight; -     byte*Data; the } Image; -  - LRESULT CALLBACK WndProc (hwnd hwnd, UINT msg, WPARAM w, LPARAM l) - { + HDC hdc; - rect rect; + Image img; a bitmapinfo bmi; at paintstruct ps; -     intirowlength; -     // -ZeroMemory (&bmi,sizeof(bitmapinfo)); -Bmi.bmiHeader.biSize =sizeof(bitmapinfoheader); -Bmi.bmiHeader.biPlanes =1; inBmi.bmiHeader.biBitCount = -; -Bmi.bmiHeader.biCompression =bi_rgb; to     // +     Switch(msg) { -      casewm_destroy: thePostQuitMessage (1); *          break; $      casewm_paint:Panax NotoginsengHDC = BeginPaint (hwnd, &ps); -GetClientRect (hwnd,&rect); theImg. Width = rect.right-rect.left; +Irowlength = ((img. width*8*3+ to) & ~ to) >>3; aImg. Height = rect.bottom-rect.top; theImg. Data = (byte*)malloc(irowlength*Img. Height); +          for(intI=0; i<irowlength*img. height;i++) -Img. data[i] = Rand ()% the; $Bmi.bmiHeader.biWidth =Img. Width; $Bmi.bmiHeader.biHeight =Img. Height; -SetDIBitsToDevice (hdc,0,0, Img. Width, Img. Height, -                           0,0,0Img. Height, Img. Data, &bmi, dib_rgb_colors); the          break; -     default:Wuyi         returndefwindowproc (hwnd, msg, w, l); the     } -     return 0; wu } -  about intMainintargcChar*Argv[]) $ { -     StaticTCHAR szappname[] = TEXT ("Randcolor"); - hwnd hwnd; - msg msg; a wndclass wndclass; +     intIcmdshow =1; theHINSTANCE hinstance =NULL; -     // $Wndclass.style = Cs_hredraw |cs_vredraw; theWndclass.lpfnwndproc =WndProc; theWndclass.cbclsextra =0; theWndclass.cbwndextra =0; theWndclass.hinstance =hinstance; -Wndclass.hicon =LoadIcon (NULL, idi_application); inWndclass.hcursor =loadcursor (NULL, idc_arrow); theWndclass.hbrbackground =(hbrush) getstockobject (white_brush); theWndclass.lpszmenuname =NULL; aboutWndclass.lpszclassname =szappname; the     if(! RegisterClass (&WNDCLASS)) { theMessageBox (NULL, TEXT ("This program requires Windows nt!"), szappname, mb_iconerror); the         return 0; +     } -hwnd = CreateWindow (szappname, TEXT ("Image"), ws_overlappedwindow^ws_thickframe, the cw_usedefault, cw_usedefault, cw_usedefault, cw_usedefault,Bayi null, null, hinstance, null); the     //the message Loop. the ShowWindow (hwnd, icmdshow); - UpdateWindow (hwnd); -  the      while(GetMessage (&msg, NULL,0,0)) { theTranslateMessage (&msg); theDispatchMessage (&msg); the     } -     return 0; the}

  

Example of using X window to display images under Linux:

1 /*2 * FILENAME:IMAGE_LINUX.C3 * Usage:tcc-lx11-run image_linux.c4 */5#include <X11/Xlib.h>6#include <X11/Xutil.h>7 8#include <stdlib.h>9 Ten #defineWIDTH 640 one #defineHEIGHT 480 a  - intMainintargcChar**Argv) - { the     intwin_b_color; -     intwin_w_color; - XEvent xev; - window window; +Visual *visual; -XImage *ximage; + GC gc; a      at     Char*buffer= (Char*)malloc(width*height*4*sizeof(Char)); -      -Display *display =Xopendisplay (NULL); -Win_b_color =blackpixel (display, defaultscreen (display)); -Win_w_color =blackpixel (display, defaultscreen (display)); -window = Xcreatesimplewindow (display,defaultrootwindow (display),0,0, WIDTH, HEIGHT,0, win_b_color, win_w_color); invisual = Defaultvisual (display,0); -     //xselectinput (display, window, exposuremask | keypressmask); to Xmapwindow (display, window); + XFlush (display); -GC = XCREATEGC (display, window,0, NULL); the     //XEvent event; *      while(1) { $          for(inti =0; I < width*height*4; i + +)Panax Notoginsengbuffer[i] = Rand ()% the; -Ximage=xcreateimage (display, visual, -, zpixmap,0, buffer,width, HEIGHT, +,0); theXputimage (display, window,gc, ximage,0,0,0,0, WIDTH, HEIGHT); +     } a     return 0; the}

The next one will introduce the knowledge of vector diagrams, which are mainly explained by Emf,svg and Postscript.

A talk on C language (ii) image display windows and Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.