在winform中使用DrawImage

时间:2012-11-10 15:45:47

标签: c++ winforms graphics

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

以下从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 );




         }
};
 }

1 个答案:

答案 0 :(得分:1)

就像编译器说的那样:'图形':不是' System :: EventArgs '的成员。 具有Graphics对象的唯一Event Arguments是PaintEventArgs,您只能使用Paint和PaintBackground。

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

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

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

只需将e->Graphics替换为graphicsInstance即可接听此电话:

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

有关绘画本身的更多细节: 正如您在文章中看到的那样,他们刚刚发布了方法并方便地将 PaintEventArgs 作为参数传递,这是覆盖 OnPaint 和 OnPaintBackground ,以减少绘制事件代码中的混乱。因此,只需确保将图形对象从两个绘制事件中的任何一个传递到辅助方法,或者如果需要,使用上面的方法创建自己的(稍微探索图形类型,您将看到可以从多个创建它来源,而不仅仅是表格的句柄)。 你可以在这里找到一篇关于代码项目的好文章,其中有一些关于在WinFroms中绘制的理论手工艺:link