简体   繁体   English

使用设备上下文在CImage对象上绘制线

[英]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. 我正在构建一个MFC c ++应用程序,在该应用程序中,我让用户读取图像,在其上画线然后保存。

so i have a "CImage" object which is called "Image" in which the user loads the image to. 所以我有一个“ CImage”对象,称为“ Image”,用户将图像加载到其中。

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. 并且我有一个设备上下文对象,并且能够使用“ OnLButtonDown”和“ OnLButtonUp”事件处理程序在运行时在设备上下文对象上画线。

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.. 然后,我让用户使用“ CImage.save”保存图像。保存了图像,但是没有设备上下文绘制的线,这是可以预期的..但是我确实希望它们出现在保存的图像中。

the question is how can i get the device context Object to affect my CImage Object? 问题是如何获取设备上下文对象来影响我的CImage对象?

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 这是我加载我的Cimage对象的地方

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

        image.Load(ImagePath);
}

and this is where i save my CImage 这是我保存我的CImage的地方

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

Are you looking for CImage:BitBlt ? 您在寻找CImage:BitBlt吗? It is used to copy a bitmap from the source device context to current device context. 它用于将位图从源设备上下文复制到当前设备上下文。

From what you've shown, it appears you are using the wrong DC. 从显示的结果看,您似乎使用了错误的DC。 You seem to be using the DC for the dialog (ie. CCLientDC) and not the actual CImage. 您似乎在对话框中使用DC(即CCLientDC),而不是实际的CImage。 You should be constructing the DC from 您应该从构建DC

CImage::GetDC () . CImage :: GetDC()

That DC will have the currently selected bitmap. 该DC将具有当前选择的位图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM