我正在尝试在winform中使用DrawImage,它在指定位置以指定大小绘制指定Image的指定部分。但是我遇到了某些错误。
error C2039: 'Graphics' : is not a member of 'System::EventArgs'
error C2227: left of '->DrawImage' must point to class/struct/union/generic type
以下代码片段取自此处,执行以下操作:
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 );
}
};
}