3

指定された画像の指定された部分を指定された場所に指定されたサイズで描画する、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  

form1.h が存在するフォルダーに system.drawing.dll を配置しました。

以下は完全なコードです。

#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 オブジェクトを持つ唯一のイベント引数は、Paint と PaintBackground でのみ取得できる PaintEventArgs です。

ここで、ボタン クリック イベントで画像を描画する必要があるため、画像を描画する表面領域のグラフィックス オブジェクトを作成する必要があります。

提供されたコードに基づいて、画像をフォームに直接描画したいと思います。次のように、フォーム ハンドルによってグラフィックス オブジェクトを作成できます。

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

この呼び出しを取得するには、次e->Graphicsのように置き換えます。graphicsInstance

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

絵画自体の詳細: あなたが提供した記事でわかるように、彼らはメソッドを投稿し、PaintEventArgsを引数として便利に渡しました。これは、 OnPaintOnPaintBackgroundをオーバーライドして混乱を減らすときに使用される典型的なパターンです。ペイント イベント コード。したがって、グラフィック オブジェクトを 2 つのペイント イベントのいずれかからヘルパー メソッドに渡すか、必要に応じて上記のメソッドを使用して独自のオブジェクトを作成するようにしてください (グラフィック タイプを少し調べてみると、複数のオブジェクトから作成できることがわかります。フォームのハンドルだけでなく、ソース)。ここで、WinFroms での描画に関するいくつかの理論作成を含むコード プロジェクトに関する素晴らしい記事を見つけることができます:リンク

于 2012-11-23T16:52:02.657 に答える