CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Apr 2005
    Posts
    41

    Writing a CDC to a jpeg file

    Hi all
    I've been trying all day to sort this out so I was hoping someone could help!!

    Basically, I am trying to write the contents of a CDC to a JPEG file and I am trying to use libjpeg.lib. Below is a simplified snippet of the code I am using at present. What I am intending the code to do is to create a picture with a square and a circle and then write this to a JPEG file. It manages to create a JPEG file with the right size however it is completely black so obviously I am doing something wrong! Any help would be greatly appreciated.

    Thanks very much for reading,

    Robbie

    (ps. I am using Visual C++ v6 and am trying to do this within a MFC dialog based project)
    Code:
    void CNucPlot::SaveFile()
    {
    	CDC myDC;
    	HBITMAP myBMP;
    	myDC.CreateCompatibleDC(NULL);
    
    	//  Set pictures size;
    	int Width = 500;
    	int Height = 500;
    
    	// Draw rectangle and an ellipse to myDC ..
    	CBrush newColour(RGB(10,10,10));
    	myDC.SelectObject(newColour);
    	CPen lColourPen (PS_SOLID, 1,RGB(50,100,150));
    	myDC.SelectObject(&lColourPen); // 100
    
    	myDC.SetBkColor(RGB(255,255,255));
    	myDC.Rectangle(10,20,100,90);
    	CRect Rect;
    	Rect.left   = 10;
    	Rect.top    = 20;
    	Rect.right  = 100;
    	Rect.bottom = 90;
    	myDC.Ellipse(Rect);
    
    	myBMP = CreateCompatibleBitmap(myDC,Width,Height);
    	SelectObject(myDC,myBMP);
    
    	struct jpeg_compress_struct cinfo;
    	struct jpeg_error_mgr jerr;
    	FILE * outfile;
    	JSAMPLE* scanline;
    	COLORREF pixel;
    
    	cinfo.err = jpeg_std_error(&jerr);
    	jpeg_create_compress(&cinfo);
    
    	outfile = fopen("robbie.jpg", "wb");
    	if(outfile == NULL) return;
    
    	jpeg_stdio_dest(&cinfo, outfile);
    	cinfo.image_width = Width;
    	cinfo.image_height = Height;
    	cinfo.input_components = 3;
    	cinfo.in_color_space = JCS_RGB;	
    	jpeg_set_defaults(&cinfo);
    
    	int quality = 90;
    	if(quality < 0) quality = 0;
    	if(quality > 100) quality = 100;
    
    	jpeg_set_quality(&cinfo, quality, FALSE);
    	jpeg_start_compress(&cinfo, TRUE);
    	scanline = new JSAMPLE[Width*3];
    
    	for(int posy = 0; posy < Height; posy++) 
    	{
    		for(int posx = 0; posx < Width; posx++)
    		{
    			pixel = GetPixel(myDC, posx, posy);
    			scanline[posx*3+0] = GetRValue(pixel);
    			scanline[posx*3+1] = GetGValue(pixel);
    			scanline[posx*3+2] = GetBValue(pixel);
    
    		}
    		jpeg_write_scanlines(&cinfo, &scanline, 1);
    	}
    	jpeg_finish_compress(&cinfo);
    	jpeg_destroy_compress(&cinfo);
    
    	delete scanline;
    	fclose(outfile);
    }
    Last edited by robbiegregg; June 5th, 2009 at 01:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured