如何在 MFC C 中保存到位图应用?

发布于 2024-11-14 21:33:24 字数 1685 浏览 1 评论 0原文

我刚刚开始使用 MFC,所以请耐心等待;)。 我已经编写了(老实说,它主要是生成的)一个简单的应用程序,它应该执行绘画杂务:绘制线条、矩形、椭圆、更改要绘制的对象的颜色等。

我需要保存在屏幕上绘制的内容成 bmp 文件。我有什么想法可以实现这一目标吗?

我不知道这是否相关,但我正在屏幕上绘制对象,而不使用任何 CBitmap 或类似的东西。这是负责绘图的代码的一部分:

CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
Anchor.x=point.x;
Anchor.y=point.y;
OldPoint.x=Anchor.x;
OldPoint.y=Anchor.y;
if(pDoc->shapeCount>=MAX_SHAPES) return;
pDoc->shapeCount++;

if(bFreehand)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new Shape;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = ePoint;
    }
if(bLine)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CLine;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = eLine;
    }
if(bRectangle)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CRectangle;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = eRectangle;
    }
if(bEllipse)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CEllipse;
    pDoc->m_shape[pDoc->shapeCount-1]->shape=eEllipse;
    }
pDoc->m_shape[pDoc->shapeCount-1]->x=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->x2=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y2=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->Pen=CurrentPen;
pDoc->m_shape[pDoc->shapeCount-1]->Brush=CurrentBrush;
bButtonDown=true;
SetCapture();

我找到了这种方法,但我不知道如何获取屏幕宽度和高度以将其填充到 CreateBitmap 参数列表中

        CBitmap *bitmap;
    bitmap.CreateBitmap(desktopW, desktopH, 1, 32, rgbData);
    CImage image;
    image.Attach(bitmap);
    image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatBMP);

I am just starting with MFC so please be tolerant ;).
I have wrote (it was mostly generated to be honest) a simple application which should do the Paint chores: drawing lines, rectangulars, ellipses, changing a color of object to be drawn etc.

I need to save what has been drawn on the screen into a bmp file. Any ideas how can I achieve this ?

I do not know if that's relevant but I am drawing objects on the screen without the use of any CBitmaps or things like that. Here is a part of code responsible for drawing :

CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
Anchor.x=point.x;
Anchor.y=point.y;
OldPoint.x=Anchor.x;
OldPoint.y=Anchor.y;
if(pDoc->shapeCount>=MAX_SHAPES) return;
pDoc->shapeCount++;

if(bFreehand)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new Shape;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = ePoint;
    }
if(bLine)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CLine;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = eLine;
    }
if(bRectangle)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CRectangle;
    pDoc->m_shape[pDoc->shapeCount-1]->shape = eRectangle;
    }
if(bEllipse)
    {
    pDoc->m_shape[pDoc->shapeCount-1] = new CEllipse;
    pDoc->m_shape[pDoc->shapeCount-1]->shape=eEllipse;
    }
pDoc->m_shape[pDoc->shapeCount-1]->x=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->x2=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y2=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->Pen=CurrentPen;
pDoc->m_shape[pDoc->shapeCount-1]->Brush=CurrentBrush;
bButtonDown=true;
SetCapture();

I have found this way to do it but I don't know how to obtain screen width and height to fill it in the CreateBitmap parameter's list

        CBitmap *bitmap;
    bitmap.CreateBitmap(desktopW, desktopH, 1, 32, rgbData);
    CImage image;
    image.Attach(bitmap);
    image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatBMP);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-11-21 21:33:24

如果您要保存的图像实际上是整个屏幕大小,则 CreateBitmap 调用仅需要桌面宽度和高度。如果这确实是您的意图,您可以使用 CWnd::GetDesktopWindow() 获取 CWnd 对象,您可以查询该对象的宽度和高度:

http://msdn.microsoft.com/en-us/library/bkxb36k8(v=VS.80).aspx

一般而言,这会变得狡猾......如果没有其他原因的话多显示器场景...所以我建议不要使用它,除非您真的想编写屏幕捕获应用程序。

您可能想要做的不是拍摄全屏截图,而只是保存程序窗口的内容。通常,您可以通过分解程序的绘图逻辑来实现此目的,以便在 Paint 方法中调用一个为获取 CDC 设备上下文而编写的辅助函数。然后,您可以在绘制调用中获得的基于窗口的 DC 上调用该函数,也可以在从位图创建的 DC 上调用该函数来进行保存。请注意,您可以在 CDC::SelectObject 中使用 CBitmap

http://msdn.microsoft.com/en-us/library/432f18e2(v=VS.71).aspx

(不过,我建议您不要使用 MFC。请尝试 Qt 代替。好多了。)

The CreateBitmap call only requires the desktop width and height if the image you wish to save is actually the entire size of the screen. If that's indeed your intent, you can use CWnd::GetDesktopWindow() to get a CWnd object that you can query for its width and height:

http://msdn.microsoft.com/en-us/library/bkxb36k8(v=VS.80).aspx

That gets dodgy in general...if for no other reason than multi-monitor scenarios...so I'd recommend against it unless you really feel like writing a screen capture app.

What you probably want to do isn't to take a full screen shot, but just save the contents of your program's window. Typically you'd do this by breaking out the drawing logic of your program so that in the paint method you call a helper function that is written to take a CDC device context. Then you can either call that function on the window-based DC you get in the paint call or on a DC you create from the bitmap to do your save. Note that you can use a CBitmap in CDC::SelectObject:

http://msdn.microsoft.com/en-us/library/432f18e2(v=VS.71).aspx

(Though let me pitch you on not using MFC. Try Qt instead. Way better.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文