3

我正在尝试在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 );




         }
};
 }
4

1 回答 1

1

就像编译器说的:' Graphics ':不是' System::EventArgs '的成员。唯一具有 Graphics 对象的事件参数是 PaintEventArgs,您只能在 Paint 和 PaintBackground 上获得它。

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

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

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

并简单地替换e->GraphicsgraphicsInstance来获得这个电话:

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

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

于 2012-11-23T16:52:02.657 回答