Windows GDI screenshot uses Libjpeg to save JPG files

Source: Internet
Author: User

/** Windows GDI screenshot save JPG file using Libjpeg */#include "stdio.h" #include "windows.h" #include "libjpeg/jpeglib.h" #pragma comment (Lib, "Libjpeg/libjpeg.lib") void Bgr_to_jpgfile (BYTE *rgb,int size,int w,int h,char *jpgfile) {struct JPEG_ERROR_MGR Jerr;jpeg_std_error (&jerr); struct Jpeg_compress_struct cinfo;cinfo.err = &jerr;jpeg_create_compress (& Cinfo); FILE *FP = fopen (Jpgfile, "wb+"); Jpeg_stdio_dest (&AMP;CINFO,FP); cinfo.image_width = W;cinfo.image_height = h; cinfo.input_components = 3;cinfo.in_color_space = Jcs_rgb;jpeg_set_defaults (&cinfo); Jpeg_set_quality (& Cinfo, +, TRUE); jpeg_start_compress (&cinfo,true); int row_stride = cinfo.image_width*cinfo.input_components; Jsamprow Row_pointer[1];while (Cinfo.next_scanline < Cinfo.image_height) {row_pointer[0] = & Rgb[cinfo.next_ Scanline * Row_stride];//row_pointer[0] = & rgb[(cinfo.image_height-cinfo.next_scanline-1) *row_stride];(void) Jpeg_write_scanlines (&cinfo, Row_pointer, 1);} Jpeg_finish_compress (&cinfo); jpeg_destRoy_compress (&cinfo); fclose (FP);} int main (int argc,char *argv) {HWND Dthwnd = GetDesktopWindow (); HDC DTHDC = GetDC (dthwnd); int dtwidth = GetSystemMetrics (sm_cxscreen); int dtheight = GetSystemMetrics (Sm_cyscreen); HDC dthdcbuf = CreateCompatibleDC (NULL); Hbitmap hbitmap = CreateCompatibleBitmap (dthdc,dtwidth,dtheight); SelectObject (Dthdcbuf, HBITMAP); BitBlt (dthdcbuf, 0, 0, dtwidth, dtheight, DTHDC, 0, 0, srccopy); int bitsize = dtwidth*dtheight*4; BYTE *bit = new Byte[bitsize]; LONG re = getbitmapbits (Hbitmap, bitsize, bit), if (bit && re) {BYTE *buf = bit; for (int i=0, j=0; j < Bitsize; i+ =3, j+=4) {* (buf+i) = * (BUF+J); * (buf+i+1) = * (buf+j+1); * (buf+i+2) = * (buf+j+2);} bgr_to_jpgfile (bit,dtwidth*dtheight*3 , Dtwidth, Dtheight, "gdi_libjpeg.jpg"); free (bit);} DeleteObject (HBITMAP);D Eletedc (DTHDCBUF); ReleaseDC (DTHWND,DTHDC); return 1;}

Gdi_jpg.cpp

Windows GDI screenshot uses Libjpeg to save JPG files

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.