gpt4 book ai didi

c++ - 在 winform 中使用 DrawImage

转载 作者:太空狗 更新时间:2023-10-29 23:07:15 26 4
gpt4 key购买 nike

我正在尝试在 winform 中使用 DrawImage,它在指定位置以指定大小绘制指定图像的指定部分。但是我遇到了某些错误。

  error C2039: 'Graphics' : is not a member of 'System::EventArgs'   
error C2227: left of '->DrawImage' must point to class/struct/union/generic type

以下代码片段取自 here , 执行以下操作:

1- 从示例文件夹中的 JPEG 文件 SampImag.jpg 创建图像。

2- 创建定义要在其中绘制图像的平行四边形的点。

3- 创建一个矩形以选择要绘制的图像部分。

4- 设置图形绘制单位为像素。

5- 将图像绘制到屏幕上。

为了在我的应用程序中使用此代码片段,我创建了一个简单的 winform 应用程序。在表单中我添加了一个按钮。单击按钮时,我希望执行以下代码:

private:
void DrawImageParaRect( PaintEventArgs^ e )
{

// Create image.
Image^ newImage = Image::FromFile( "SampImag.jpg" );

// Create parallelogram for drawing image.
Point ulCorner = Point(100,100);
Point urCorner = Point(325,100);
Point llCorner = Point(150,250);
array<Point>^ destPara = {ulCorner,urCorner,llCorner};

// Create rectangle for source image.
Rectangle srcRect = Rectangle(50,50,150,150);
GraphicsUnit units = GraphicsUnit::Pixel;

// Draw image to screen.
e->Graphics->DrawImage( newImage, destPara, srcRect, units );
}

在我创建的应用程序中,我得到了以下几行按钮:

  #pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{


}

所以我将代码放在这个事件中,如下所示:

 #pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{


// Create image.
Image^ newImage = Image::FromFile( "SampImag.jpg" );

// Create parallelogram for drawing image.
Point ulCorner = Point(100,100);
Point urCorner = Point(325,100);
Point llCorner = Point(150,250);
array<Point>^ destPara = {ulCorner,urCorner,llCorner};

// Create rectangle for source image.
Rectangle srcRect = Rectangle(50,50,150,150);
GraphicsUnit units = GraphicsUnit::Pixel;

// Draw image to screen.
e->Graphics->DrawImage( newImage, destPara, srcRect, units );
}

如何消除错误?

   error C2039: 'Graphics' : is not a member of 'System::EventArgs'  
error C2227: left of '->DrawImage' must point to class/struct/union/generic type

我已将 system.drawing.dll 放在我的 form1.h 所在的文件夹中。

完整代码如下:

#include "stdafx.h"
#include <stdio.h>
#using <system.drawing.dll>




using namespace System;
using namespace System::Drawing;


#pragma once

namespace Zooming_10Nov {


using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(120, 91);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{






// Create image.
Image^ newImage = Image::FromFile( "SampImag.jpg" );

// Create parallelogram for drawing image.
Point ulCorner = Point(100,100);
Point urCorner = Point(325,100);
Point llCorner = Point(150,250);
array<Point>^ destPara = {ulCorner,urCorner,llCorner};

// Create rectangle for source image.
Rectangle srcRect = Rectangle(50,50,150,150);
GraphicsUnit units = GraphicsUnit::Pixel;

// Draw image to screen.
e->Graphics->DrawImage( newImage, destPara, srcRect, units );




}
};
}

最佳答案

正如编译器所说:“Graphics”:不是“System::EventArgs”的成员。唯一具有图形对象的事件参数是 PaintEventArgs,您只能在 Paint 和 PaintBackground 上获得它。

现在,由于您想在按钮单击事件上绘制图像,因此您必须为要在其上绘制图像的表面区域创建图形对象。

根据提供的代码,我猜测您希望将图像直接绘制到表单中。您可以通过表单句柄创建图形对象,如下所示:

Graphics^ graphicsInstance = Graphics::FromHwnd(this->Handle);

只需将 e->Graphics 替换为 graphicsInstance 即可获得此调用:

graphicsInstance->DrawImage( newImage, destPara, srcRect, units );

关于这幅画本身的更多细节:正如您在所提供的文章中看到的那样,他们刚刚发布了方法并方便地将 PaintEventArgs 作为参数传递,这是覆盖 OnPaintOnPaintBackground 以减少绘画事件代码中的困惑。因此,只需确保将图形对象从两个绘制事件中的任何一个传递给您的辅助方法,或者如果有必要,请使用上面的方法创建您自己的图形对象(稍微探索一下图形类型,您会发现您可以从多个创建它源,而不仅仅是表单的句柄)。您可以在此处找到一篇关于代码项目的好文章,其中包含一些关于在 WinFroms 中绘图的理论制作:link

关于c++ - 在 winform 中使用 DrawImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com