VC + + Display Picture: __c++

Source: Internet
Author: User
Tags textout
VC + + Display Pictures:
2 days of study, 48 hours finally finished,




To put it simply, the original is to use the VC display picture,
Dc.draw (...); was solved,


As the project progresses, the pictures need to be saved,
Dc.selectobject (&bmp);
Savebmp (BMP, "c:\a.bmp");




I wanted to do this, and later found that the PDF needed to use JPG
Well, then, we've got another class CImage.
CIMAGE img;
Img.load ("C:\a.bmp");
Img.safe ("c:\a.jpg"); The result is open,
Virginity, oh, no, it's "distorted," too serious,


and found a cow to write the safejpg, the process is too tortuous, a few 10 hours passed
CIMAGE img;
Img.load ("C:\a.bmp");
Img.safejpeg ("C:\a.jpg", 100); The second parameter is sharpness
Can barely see people, distortion is not very serious, barely able to see;




Then do it, is the background program I this is to rely on the interface out of the live,
How Ah, found the Sun Xin Teacher 11 Lesson code, found the CMetaFileDC,
M_dcmetafile.create ("c:\\create.wmf");
M_dcmetafile.settextcolor (0xFF);
M_dcmetafile.textout (0, 0, "I am Metafile");

Hmetafile Hmetfile;
Hmetfile = M_dcmetafile.close ();
Save compatible DC
Copymetafile (Hmetfile, "c:\\copymetafile.wmf");


A look at the effect is very good,
Only the format WMF, to be loaded PDF words the road quite twists and turns,








And then I thought, the CDC can show in the interface why it can't be displayed in memory
So the code for the memory drawing appears
CDC MEMDC;
MEMDC. CreateCompatibleDC (&m_dcmetafile);
MEMDC. PlayMetaFile (Hmetfile);
The WMF file was played out,


CBitmap Bitmap;
Bitmap. CreateCompatibleBitmap (&m_dcmetafile,rect. Width (), Rect. Height ());
MEMDC. SelectObject (&BITMAP);
And then execute
Safejpeg (Bitmap, "c:\ok.jpg");


The effect is what I want, but only one color.


Do not worry again shun Shun thought:


1. Create a compatible DC
CDC MEMDC;
MEMDC. CreateCompatibleDC (DC);


2. Creating a BMP Bitmap (CSDN) that creates an empty graph results in monochrome
/*http://bbs.csdn.net/topics/90080731*/
CBitmap bmp;
Bmp. CreateCompatibleBitmap (MEMDC, 400, 566);


3. Select Bitmap
MEMDC. SelectObject (&bmp);


4. Write
Memdc.textout (0,0, "0k");


5. Save JPG
Safejpeg (Bitmap, "c:\ok.jpg");




Open Look, a color
Finally found the method, only need to modify the BMP associated compatible bitmap is done,
Create a BMP associated with the DC, rather than a very MEMDC associated CBitmap object,
In fact, this method Fishjam already said in Csdn, but I have not seen the clue
Finally find the same results as others, alas, ashamed of AH
http://blog.csdn.net/fishjam/article/details/7400627
---------------------------------------------------------------------------




Hbitmap hbit;
CRect rect (0,0,400,566);


cdc* DC = GetDC ();


CDC MEMDC;
MEMDC. CreateCompatibleDC (DC);


CBitmap bmp;
Bmp. CreateCompatibleBitmap (DC, 400, 566);

:: GetObject (&MEMDC, sizeof (hbit), &hbit);

MEMDC. SelectObject (&bmp);

CBrush Brush;
Brush. CreateSolidBrush (RGB (255,255,255));
MEMDC. SelectObject (&brush);
MEMDC. FillRect (CRect (0,0,400,566), &brush);


MEMDC. Ellipse (0,0,400,566);

MEMDC. SetTextColor (0xFF);
MEMDC. MoveTo (0, 0);
MEMDC. LineTo (100,100);
MEMDC. TextOut (50,50, "I want the Red font");
Dc->bitblt (0,0,400,566, &MEMDC, 0, 0, srccopy);
Savebmp (BMP, "c:\\memdc.bmp");










-----------------------------------
VC under the Hbitmap Save as BMP pictures
BOOL savebmp (hbitmap hbitmap, CString FileName)
{
HDC HDC;
The number of bytes per pixel at the current resolution
int ibits;
The number of bytes per pixel in a bitmap
WORD Wbitcount;
Define palette size, pixel byte size in bitmap, bitmap file size, number of bytes written to file
DWORD dwpalettesize=0, Dwbmbitssize=0, dwdibsize=0, dwwritten=0;
Bitmap Property Structure
BITMAP BITMAP;
Bitmap File Header structure
Bitmapfileheader Bmfhdr;
Bitmap Information Header structure
Bitmapinfoheader bi;
Point to bitmap information header structure
Lpbitmapinfoheader LPBI;
Definition file, allocating memory handle, palette handle
HANDLE FH, Hdib, Hpal,holdpal=null;


Calculates the number of bytes per pixel in a bitmap file
HDC = CreateDC (_t ("DISPLAY"), NULL, NULL, NULL);
Ibits = GetDeviceCaps (HDC, Bitspixel) * GetDeviceCaps (HDC, planes);
DeleteDC (HDC);
if (ibits <= 1)
Wbitcount = 1;
else if (ibits <= 4)
Wbitcount = 4;
else if (ibits <= 8)
Wbitcount = 8;
Else
Wbitcount = 24;


GetObject (hbitmap, sizeof (BITMAP), (LPSTR) &bitmap);
bi.bisize= sizeof (Bitmapinfoheader);
Bi.biwidth = Bitmap.bmwidth;
Bi.biheight = Bitmap.bmheight;
Bi.biplanes = 1;
Bi.bibitcount = Wbitcount;
Bi.bicompression= Bi_rgb;
bi.bisizeimage=0;
Bi.bixpelspermeter = 0;
Bi.biypelspermeter = 0;
bi.biclrimportant = 0;
bi.biclrused = 0;


Dwbmbitssize = (bitmap.bmwidth *wbitcount+31) *4* bitmap.bmheight;


allocating memory for bitmap content
Hdib = GlobalAlloc (ghnd,dwbmbitssize+dwpalettesize+sizeof (Bitmapinfoheader));
LPBI = (lpbitmapinfoheader) globallock (HDIB);
*LPBI = bi;


Working with palettes
Hpal = Getstockobject (Default_palette);
if (hpal)
{
HDC =:: GetDC (NULL);
Holdpal =:: SelectPalette (HDC, (Hpalette) hpal, FALSE);
RealizePalette (HDC);
}


Gets the new pixel value under the palette
GetDIBits (Hdc,hbitmap, 0, (UINT) Bitmap.bmheight,
(LPSTR) lpbi+ sizeof (Bitmapinfoheader) +dwpalettesize,
(Bitmapinfo *) LPBI, dib_rgb_colors);


Restore Palette
if (Holdpal)
{
:: SelectPalette (HDC, (Hpalette) Holdpal, TRUE);
RealizePalette (HDC);
:: ReleaseDC (NULL, HDC);
}


Create a bitmap file
FH = CreateFile (FileName, generic_write,0, NULL, Create_always,
File_attribute_normal | File_flag_sequential_scan, NULL);


if (fh = = INVALID_HANDLE_VALUE) return FALSE;


Set Bitmap file Headers
Bmfhdr.bftype = 0x4d42; "BM"
dwdibsize = sizeof (Bitmapfileheader) +sizeof (bitmapinfoheader) +dwpalettesize+dwbmbitssize;
Bmfhdr.bfsize = dwdibsize;
bmfhdr.bfreserved1 = 0;
Bmfhdr.bfreserved2 = 0;
Bmfhdr.bfoffbits = (DWORD) sizeof (Bitmapfileheader) + (DWORD) sizeof (Bitmapinfoheader) +dwpalettesize;
Write Bitmap file headers
WriteFile (FH, (LPSTR) &bmfhdr, sizeof (Bitmapfileheader), &dwwritten, NULL);
Write the rest of the bitmap file
WriteFile (FH, (LPSTR) LPBI, Dwdibsize, &dwwritten, NULL);
Clear
GlobalUnlock (HDIB);
GlobalFree (HDIB);
CloseHandle (FH);




Save As JPG
Savejpg (FileName);


return TRUE;
}




--------------------------------------------------
The source written by high man is added to the people.


/************************************************************
Increase
*//Save as JPEG file, quality (1..100) for image quality
* HRESULT Cimage::savejpeg (LPCTSTR pszfilename,int quality)
*
* Evlon
* http:evlon.cnblogs.com
* 273352165
*
*************************************************************/


Inline HRESULT cimage::savejpeg (LPCTSTR pszfilename,int quality) const throw ()
{
LONG paramvalue = quality;
Gdiplus::encoderparameter EP;
Ep. Guid = gdiplus::encoderquality;
Ep. NumberOfValues = 1;
Ep. Type = 4;//gdiplus::encoderparametervaluetype::encoderparametervaluetypelong;
Ep. Value = &paramValue;


Gdiplus::encoderparameters EPS;
Eps. Count = 1;
Eps. Parameter[0] = EP;
Return Save (pszfilename,gdiplus::imageformatjpeg,&eps);
}


























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.