简体   繁体   English

如何在 MFC C++ 应用程序中保存到 bitmap?

[英]How to save to a bitmap in MFC C++ application?

I am just starting with MFC so please be tolerant;).我只是从 MFC 开始,所以请宽容;)。 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.我已经写了(老实说,它主要是生成的)一个简单的应用程序,它应该完成绘制工作:绘制线条、矩形、椭圆、更改要绘制的 object 的颜色等。

I need to save what has been drawn on the screen into a bmp file.我需要将屏幕上绘制的内容保存到 bmp 文件中。 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.我不知道这是否相关,但我在屏幕上绘制对象而不使用任何 CBitmap 或类似的东西。 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我找到了这种方法,但我不知道如何获取屏幕宽度和高度以将其填充到 CreateBitmap 参数列表中

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

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.如果您要保存的图像实际上是屏幕的整个大小,则CreateBitmap调用仅需要桌面宽度和高度。 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:如果这确实是您的意图,您可以使用CWnd::GetDesktopWindow()获取CWnd object ,您可以查询其宽度和高度:

http://msdn.microsoft.com/en-us/library/bkxb36k8(v=VS.80).aspx 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.您可能想要做的不是全屏截图,而只是保存程序 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.通常,您会通过中断程序的绘图逻辑来做到这一点,以便在绘制方法中调用帮助程序 function,该帮助程序是为获取CDC设备上下文而编写的。 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.然后,您可以在绘制调用中获得的基于窗口的 DC 或从 bitmap 创建的 DC 上调用 function 来进行保存。 Note that you can use a CBitmap in CDC::SelectObject :请注意,您可以在CDC::SelectObject中使用CBitmap

http://msdn.microsoft.com/en-us/library/432f18e2(v=VS.71).aspx 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.) (尽管让我告诉你不要使用 MFC。试试ZE8801102A40AD89DDCFDCAEBF008D25Z 。好多了。)

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

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