绘制图像和保存

时间:2014-06-06 22:24:45

标签: image visual-c++ graphics bitmap c++-cli

我想我真的不明白图形对象在Visual C ++ 2010 Express中是如何工作的。

我从网络摄像头抓取一个框架,并在其上画一个圆圈。它在屏幕上运行良好。我只是创建一个Graphics对象,绘制图像,然后绘制椭圆。

在pictureBox_paint函数中,我有

Graphics^ g = e->Graphics; // from the camera 

System::Drawing::Rectangle destRect = System::Drawing::Rectangle(0,0,pbCameraMonitor->Size.Width,pbCameraMonitor->Size.Height);
double slitHeightToWidth = 3;
g->DrawImage(this->currentCamImage,destRect);
int circleX, circleY;
circleX = (int) (pbCameraMonitor->Size.Width - radius/slitHeightToWidth)/2;
circleY = (int) (pbCameraMonitor->Size.Height - radius)/2;

g->DrawEllipse(Pens::Red, circleX, circleY, (int) radius/slitHeightToWidth, (int) radius);

到目前为止,我的椭圆很好地被绘制在那里。 destRect位确保它被缩放到pictureBox大小。每次相机报告新图像时,我都会使pictureBox无效,而且我有视频。

现在,在按钮上单击我想保存此图像,上面有红色椭圆。但是,我不想要屏幕上显示的重新缩放版本,我想要完整的res版本。所以,我会把另一个帧抓到一个名为" grabbedFrame"" grabbedFrame"并这样做:

String ^photofile = "Image_" + expRecord.timestamp.ToString("s") + ".jpg"; // get a unique filename
photofile = photofile->Replace(':', '_');
Graphics^ g = Graphics::FromImage(grabbedFrame);
g->DrawEllipse(Pens::Red, 20, 20, 20, 20);  // circle size fixed just for demo
grabbedFrame->Save(photofile, System::Drawing::Imaging::ImageFormat::Jpeg);

当我这样做时,我得到了没有红色圆圈的图像保存。

g-> DrawEllipse实际上是否修改了Bitmap?或者只包含要绘制的Bitmap +指令?如果是后者,pictureBox如何知道Bitmap已被修改?如果是前者,为什么我的保存不包含修改?

如何保存修改后的位图?

1 个答案:

答案 0 :(得分:0)

您需要将加载的图像绘制到新的Bitmap中,对该位图进行修改,然后保存。

像(伪代码' ish):

// create bitmap and get its graphics
Bitmap^ pBmp = gcnew Bitmap(grabbedFrame->Width, grabbedFrame->Height);
Graphics^ g = Graphics::FromImage(pBmp);

// draw grabbed frame into bitmap
g->DrawImage(grabbedFrame, 0, 0, grabbedFrame->Width, grabbedFrame->Height);

// draw other stuff
g->DrawEllipse(Pens::Red, 20, 20, 20, 20);

// save the result
pBmp->Save(photofile, System::Drawing::Imaging::ImageFormat::Jpeg);