CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    515

    The pen is too thick in memory DC! How can I get single pixel pen?

    I am using MM_ANIOSTROPIC mode in memory device context. I create a single pixel pen but it is too thick. I than tried to set just one pixel and that one pixel is also just thick! How can I use just a single pixel pen? You can see in the attach image the text drawn is so big.
    Code:
    #define BMP_WIDTH		400 
    #define BMP_HEIGHT		200
    Code:
    void CDrawDlg::Draw(CDC * pDC)
    {
    	CBitmap bitmap;
    	bitmap.CreateCompatibleBitmap(pDC, BMP_WIDTH, BMP_HEIGHT);
    
    	CDC dcMem;
    	dcMem.CreateCompatibleDC(pDC);
    
    	CBrush brush (RGB(0,0,255));
    	CBitmap * pOldBitmap = dcMem.SelectObject(&bitmap);
    
    	BITMAP BM;
    	bitmap.GetBitmap(&BM);
    
    	dcMem.FillRect(CRect (0, 0, BM.bmWidth, BM.bmHeight), &brush);
    
    
    	dcMem.SetMapMode(MM_ANISOTROPIC);
    	dcMem.SetViewportExt(BMP_WIDTH, BMP_HEIGHT);
    	dcMem.SetWindowExt(400 * 4, 200 * 4);
    	
    	// select a single pixel pen
    	CPen pen(PS_SOLID | PS_COSMETIC, 0, RGB(255,0,0));
    	//CPen pen;
    	//pen.CreateStockObject(BLACK_PEN);
    	CPen* oldPen =  dcMem.SelectObject(&pen);
    
    	dcMem.SetPixel(0, 100 , RGB(255, 0, 0)); // a test, even this one pixel is just too thick in the output!
    
    
    	dcMem.TextOut(0,0, _T("(0,0)")); // draw something at the new center
    	dcMem.TextOut(30,30, _T("(30,30)"));
    
    	dcMem.MoveTo(0, 0);
    	dcMem.LineTo(BMP_WIDTH, BMP_HEIGHT);
    
     
    	pDC->BitBlt(50, 50, BM.bmWidth, BM.bmHeight, &dcMem, 0, 0, SRCCOPY);
    
    	dcMem.SelectObject(pOldBitmap);
    	dcMem.SelectObject(oldPen);
    }
    If I change the call to dcMem.SetWindowExt(400, 200); than everything is drawn correctly in correct sizes. I just don't want text to scale per MM_ANISOTROPIC mode and use fixed 1 pixel ones. It works in regular dc but not so in memory dc. Any pointers please?
    Attached Images Attached Images

  2. #2
    Join Date
    Jul 2005
    Posts
    767

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    I do not understand your requirement, however I believe the problem lies in SetWindowExt API as you rightly pointed. Note that Viewport is the area of the DC which the user should see, however in this case since the DC is limited to 400 by 200 pixels so when you set the window extends (DC size) beyond the initialized size you would have Magnification.
    One's mistake cannot be your excuse!

  3. #3
    Join Date
    Aug 2006
    Posts
    515

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    Quote Originally Posted by MrBeans
    I do not understand your requirement.
    The requirment is the gif attached, this is the result I want in which area is still 400 * 200 pixels and text and lines are plotted correcly with one pixel pen. I get this result with:
    Code:
    	dcMem.SetWindowExt(400, 200);
    	dcMem.SetViewportExt(BMP_WIDTH, BMP_HEIGHT);
    My actual logical coordinates range is large think 5000 to -5000 but when I use those, the pen size enlarges which I still want to be fixed to 1 pixel otherwise it is just unpractical to draw anything.

    So the problem is that I need to have a fixed 400 * 200 pixels plotting area (device coordinates) and I need to draw something in it which for simplictiy say is 1600, 800 and I still want to use single pixel pen otherwise the drawing does not make sense (undesirably thick).

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    MM_ANISOTROPIC scales everything to the viewport window.

    As an experiment, try setting the mapping mode to MM_TEXT when you draw the single pixel (or a test line), and then set the mapping mode back to MM_AISOTROPIC

    Mike

  5. #5
    Join Date
    Aug 2006
    Posts
    515

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    Quote Originally Posted by MikeAThon
    MM_ANISOTROPIC scales everything to the viewport window.

    As an experiment, try setting the mapping mode to MM_TEXT when you draw the single pixel (or a test line), and then set the mapping mode back to MM_AISOTROPIC

    Mike
    I set back the MM_TEXT mode after the drawing code but just before BitBlt() and surprisingly it fixed the problem. I don't know how it did but I will take it!

    Is there any explanation for this though? Is it required to set back the old mapping mode? After all this is what I did.
    Code:
    int iOldMode = dcMem.SetMapMode(MM_ANISOTROPIC);
    
    // now use dcMem to draw anything e.g dcMem.LineTo(), dcMem.TextOut()
    [drawing code here]
    
    // at the end set back the old mappig mode
    dcMem.SetMapMode(iOldMode);
    
    pDC->BitBlt(50, 50, BM.bmWidth, BM.bmHeight, &dcMem, 0, 0, SRCCOPY);

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    I don't have any explanations, sorry.

    I have noticed that problems with MM_ANISOTROPIC sometimes are caused with the video driver, and not your code. As an example, I once had a problem with MM_ANISOTROPIC, as described here: "Inconsistent Pen Positioning with MM_ANISOTROPIC" at http://www.codeguru.com/forum/showthread.php?t=398354 . It turned out the code was fine but there was a flaw in the video driver. The code worked fine in machines other than my development machine.

    So, one suggestion is to try various versions of your code on machines with different video drivers/video cards

    Mike

  7. #7
    Join Date
    Aug 2006
    Posts
    515

    Re: The pen is too thick in memory DC! How can I get single pixel pen?

    Thanks for the post, just to add on, I am having unpredictable results as well depending on the output device in particular with the text/font size.

    1. If I do print preview, the text size and everything is perfect.
    2. If I send it to printer, the text size alone is just way too big like 36 font size.
    3. If I direct the print output to pdf file, no text is shown at all but everything else is good (drawing).

    I don't know why would results warry for each device, atleast I expect them to be consistant even if not right.

    The print preview indicates that things are drawn properly in memory dc (hence correct results). Now all I do is BitBlt() it to the main dc where the text scrambles depending on the device! I am looking into it but any pointers are welcome.

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