Mohab Mesherf
Mohab Mesherf

Reputation: 13

Draw lines using Device Context over a CImage object

Im building a MFC c++ application in which i let the user read an image, draw lines on it and then save it.

so i have a "CImage" object which is called "Image" in which the user loads the image to.

and i have a device context object and i was able to draw lines on it the device context object that is in run-time using "OnLButtonDown" and "OnLButtonUp" event handlers.

i then let the user save the image using "CImage.save" .. the image is saved but the device context drawn lines aren't there which is to be expected .. but I DO want them to appear in the saved image..

the question is how can i get the device context Object to affect my CImage Object?

this is the event handler for mouse button down

void CProFilterDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    curser =point;
    if (draw && Boundry.PtInRect(point) )
    {
        CDialogEx::OnLButtonDown(nFlags, point);
    }

}

and this one when the mouse button is up

void CProFilterDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
    if (draw && Boundry.PtInRect(curser) && Boundry.PtInRect(point))
    {
        CClientDC dc(this);
        dc.MoveTo(curser);
        dc.LineTo(point);
        CDialogEx::OnLButtonUp(nFlags, point);
    }


}

this is where i load my Cimage object

void CProFilterDlg::OnBnClickedBtnBrowse()
{
    CFileDialog Browse(true);
    if(Browse.DoModal() == IDOK)
    {
         ImagePath = Browse.GetPathName();
    }

        image.Load(ImagePath);
}

and this is where i save my CImage

void CProFilterDlg::OnBnClickedSave()
{
    CFileDialog Save(true);
    if(Save.DoModal() == IDOK)
    {
        ImagePath = Save.GetPathName();
    }
    image.Save(ImagePath,Gdiplus::ImageFormatBMP);
}

Upvotes: 0

Views: 1110

Answers (2)

rrirower
rrirower

Reputation: 4590

From what you've shown, it appears you are using the wrong DC. You seem to be using the DC for the dialog (ie. CCLientDC) and not the actual CImage. You should be constructing the DC from

CImage::GetDC ().

That DC will have the currently selected bitmap.

Upvotes: 1

Kryomaani
Kryomaani

Reputation: 149

Are you looking for CImage:BitBlt? It is used to copy a bitmap from the source device context to current device context.

Upvotes: 0

Related Questions