C++ 在winform中使用DrawImage

C++ 在winform中使用DrawImage,c++,winforms,graphics,C++,Winforms,Graphics,我试图在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 以下代码段取自,执行以下操作: 1-从示例文件夹中的JPEG文件S

我试图在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   
以下代码段取自,执行以下操作:

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)      
 {   


  // 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 );
  }
我已将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 );




         }
};
 }
#包括“stdafx.h”
#包括
#使用
使用名称空间系统;
使用名称空间系统::绘图;
#布拉格语一次
名称空间缩放{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
/// 
///表格1摘要
/// 
public ref类Form1:public System::Windows::Forms::Form
{
公众:
表格1(无效)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~Form1()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::按钮^button1;
受保护的:
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
此->按钮1=(gcnew System::Windows::Forms::Button());
此->SuspendLayout();
// 
//按钮1
// 
此->按钮1->位置=系统::图纸::点(120,91);
此->按钮1->名称=L“按钮1”;
该->按钮1->尺寸=系统::图纸::尺寸(75,23);
此->按钮1->选项卡索引=0;
此->按钮1->文本=L“按钮1”;
此->按钮1->UseVisualStyleBackColor=true;
此->按钮1->Click+=gcnewsystem::EventHandler(此,&Form1::按钮1\u Click);
// 
//表格1
// 
这->自动缩放尺寸=系统::图纸::尺寸(6,13);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(292273);
此->控制->添加(此->按钮1);
此->名称=L“Form1”;
此->文本=L“Form1”;
此->恢复布局(错误);
}
#布拉格端区
私有:系统::无效按钮1\u单击(系统::对象^sender,系统::事件参数^e)
{
//创建图像。
Image^newImage=Image::FromFile(“SampImag.jpg”);
//为绘图图像创建平行四边形。
点ulCorner=点(100100);
点urCorner=点(325100);
点LL角=点(150250);
数组^destPara={ulCorner,urCorner,llCorner};
//为源图像创建矩形。
矩形srcRect=矩形(50,50150);
GraphicsUnits=GraphicsUnits::像素;
//将图像绘制到屏幕上。
e->Graphics->DrawImage(新建图像、destPara、srcRect、单位);
}
};
}

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

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

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

Graphics^ graphicsInstance = Graphics::FromHwnd(this->Handle);
只需将
e->Graphics
替换为
graphicsInstance
即可获得此呼叫:

graphicsInstance->DrawImage( newImage, destPara, srcRect, units );
关于这幅画本身的更多细节: 正如您在提供的文章中所看到的,他们刚刚发布了该方法,并方便地将painteventags作为参数传递,这是覆盖OnPaintOnPaintBackground以减少绘制事件代码混乱时使用的典型模式。因此,只需确保将图形对象从两个绘制事件中的任何一个传递给您的助手方法,或者在必要时使用上述方法创建您自己的图形对象(稍微探索一下图形类型,您会发现您可以从多个源创建它,而不仅仅是表单句柄)。
你可以在这里找到一篇关于代码项目的好文章,其中有一些关于WinFroms绘图的理论:

这从根本上说是一种错误的方法。有很多关于Winforms编程的好书。从您当地的库中选择一个,以避免反复试验。我正在调试一个由其他人编写的100000行代码,其中我必须添加一个缩放的小功能,而且我近期没有计划开发winform应用程序。那么,这将如何证明我有时间读书呢。这方面的专家可以给我指一些相关的文章。这也会有帮助。提前感谢您宝贵的时间。。
Graphics^ graphicsInstance = Graphics::FromHwnd(this->Handle);
graphicsInstance->DrawImage( newImage, destPara, srcRect, units );