質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2578閲覧

Unity using エラー script

1236

総合スコア19

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/01/06 08:19

編集2021/01/14 11:29

前提・実現したいこと

https://qiita.com/nkojima/items/1fb901c13f9707808bc0
unityで上記サイトの『2つの画像の差分を得る』のプログラムを実行したい。

発生している問題・エラーメッセージ

ImageComparator.cs の using System.Drawing.Imaging;でエラーが出ます。

Assets/script/ImageComparator.cs(4,22): error CS0234: The type or namespace name 'Imaging' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)

該当のソースコード

C#

1ImageComparator.cs 2 3using System; 4using System.Collections.Generic; 5using System.Drawing; 6using System.Drawing.Imaging; 7using System.Text; 8 9namespace CSharpStudy.Image 10{ 11 /// <summary> 12 /// 「画像比較機」クラス。 13 /// </summary> 14 public class ImageComparator 15 { 16 /// <summary> 17 /// 1ピクセルずつ画像を比較して、差分の画像を返す。 18 /// </summary> 19 /// <param name="bmp1Path">比較する画像1のファイルパス。</param> 20 /// <param name="bmp2Path">比較する画像2のファイルパス。</param> 21 /// <param name="path">差分画像の保存先となるファイルパス。</param> 22 /// <returns>2つの画像が同じであればtrue、そうでなければfalseを返す。</returns> 23 public static bool Compare(string bmp1Path, string bmp2Path, string path = @".\diff_image.png") 24 { 25 bool isSame = true; 26 27 // 画像を比較する際に「大きい方の画像」のサイズに合わせて比較する。 28 Bitmap bmp1 = new Bitmap(bmp1Path); 29 Bitmap bmp2 = new Bitmap(bmp2Path); 30 int width = Math.Max(bmp1.Width, bmp2.Width); 31 int height = Math.Max(bmp1.Height, bmp2.Height); 32 33 Bitmap diffBmp = new Bitmap(width, height); // 返却する差分の画像。 34 Color diffColor = Color.Red; // 画像の差分に付ける色。 35 36 // 全ピクセルを総当りで比較し、違う部分があればfalseを返す。 37 for (int i = 0; i < width; i++) 38 { 39 for (int j = 0; j < height; j++) 40 { 41 try 42 { 43 Color color1 = bmp1.GetPixel(i, j); 44 if (color1 == bmp2.GetPixel(i, j)) 45 { 46 diffBmp.SetPixel(i, j, color1); 47 } 48 else 49 { 50 diffBmp.SetPixel(i, j, diffColor); 51 isSame = false; 52 } 53 } 54 catch 55 { 56 // 画像のサイズが違う時は、ピクセルを取得できずにエラーとなるが、ここでは「差分」として扱う。 57 diffBmp.SetPixel(i, j, diffColor); 58 isSame = false; 59 } 60 } 61 } 62 diffBmp.Save(path, ImageFormat.Png); 63 return isSame; 64 } 65 } 66}

C#

1Program.cs 2 3using System.IO; 4using CSharpStudy.Image; 5using UnityEngine; 6 7namespace CSharpStudy { 8 class Program : MonoBehaviour { 9 void Start() { 10 // Assets/Textures/one.png を使う 11 string BITMAP1_PATH = Path.Combine(Application.dataPath, "images", "CameraScreenShot.png"); 12 // Assets/Textures/two.png を使う 13 string BITMAP2_PATH = Path.Combine(Application.dataPath, "images", "CameraScreenShot2.png"); 14 // 差分画像を Assets/Texturesフォルダ に diff.png という名前で保存 15 string DIFF_IMG_PATH = Path.Combine(Application.dataPath, "images", "diff.png"); 16 //private const string BITMAP1_PATH = @"Assets/images/CameraScreenShot.png"; 17 //private const string BITMAP2_PATH = @"Assets/images/CameraScreenShot2.png"; 18 //private const string DIFF_IMG_PATH = @"Assets/images/diff_image.png"; 19 20 21 bool isSame = ImageComparator.Compare(BITMAP1_PATH, BITMAP2_PATH, DIFF_IMG_PATH); 22 23 if (isSame) { 24 Debug.Log("2つの画像は同じです。"); 25 } else { 26 Debug.Log("2つの画像は異なります。"); 27 Debug.Log("次の差分ファイルを確認してください。:" + DIFF_IMG_PATH); 28 } 29 } 30 } 31}

補足情報(FW/ツールのバージョンなど)

英語で読めませんが同じ質問(?)

https://stackoverflow.com/questions/43017826/using-system-drawing-imaging-imaging-does-not-exist-in-the-system-drawing

###追記
config
ファイル

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Using System.Drawing.Image works but shows as error
ここで回答される内容を参考に試してみたらエラーが消えて実行できるようになり、
差分画像の作成が出来ました!

環境
Unity 2019.4.11f1

  1. Assetsの下にPluginsフォルダを作成
  2. ApiCompatibilityLevelを.Net4.xに変える

イメージ説明

  1. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0に"System.Drawing.dll"があるはずなのでPluginsフォルダにコピペ

イメージ説明

#・使用した画像
イメージ説明
イメージ説明

#・差分結果
イメージ説明

追記

C#

1using System.IO; 2using CSharpStudy.Image; 3using UnityEngine; 4 5namespace CSharpStudy { 6 class Program : MonoBehaviour { 7 void Start() { 8 // Assets/Textures/one.png を使う 9 string BITMAP1_PATH = Path.Combine(Application.dataPath, "Textures", "one.png"); 10 // Assets/Textures/two.png を使う 11 string BITMAP2_PATH = Path.Combine(Application.dataPath, "Textures", "two.png"); 12 // 差分画像を Assets/Texturesフォルダ に diff.png という名前で保存 13 string DIFF_IMG_PATH = Path.Combine(Application.dataPath, "Textures", "diff.png"); 14 15 bool isSame = ImageComparator.Compare(BITMAP1_PATH, BITMAP2_PATH, DIFF_IMG_PATH); 16 17 if (isSame) { 18 Debug.Log("2つの画像は同じです。"); 19 } else { 20 Debug.Log("2つの画像は異なります。"); 21 Debug.Log("次の差分ファイルを確認してください。:" + DIFF_IMG_PATH); 22 } 23 } 24 } 25}

投稿2021/01/08 14:27

編集2021/01/13 18:04
lazh

総合スコア300

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

1236

2021/01/11 06:20

"System.Drawing.dll"がどこにあるかわからないです。 Macを使用しているのですが可能でしょうか?
lazh

2021/01/11 09:47 編集

mac持ってないので「これだ」って方法を示すことが出来ないですが http://answers.unity.com/comments/1397785/view.html Best Answerのスレッドに > On macOS 10.12 with Unity 5.6.1f1, System.Drawing.dll was in > /Applications/Unity/Unity.app/Contents/$$anonymous$$ono/lib/mono/2.0. とあるのでここ付近にあるかもしれないです http://leeaps.com/2014/12/mac/2182/ 「パッケージの内容を表示」から探してみてください
lazh

2021/01/12 10:23

まだ試している最中でしょうか mac試す機会があったのでちょっとやってみました libgdiplusが必要みたいなのでターミナルから > brew install mono-libgdiplus と入力してインストール そして、(使っているバージョンの)Unity.App/Contents/MonoBleedingEdge/etc/configファイルを編集します </configuration>の上に ------ <dllmap dll="gdiplus" target="/Library/Frameworks/Mono.framework/Versions/XXXX/lib/libgdiplus.dylib" /> <dllmap dll="gdiplus.dll" target="/Library/Frameworks/Mono.framework/Versions/XXXX/lib/libgdiplus.dylib" /> ------ と入力 XXXXは/Library/Frameworks/Mono.framework/Versions/以下にあるフォルダの名前を入れる 自分の時は6.12.0でした (使っているバージョンの)Unity.App/Contents/MonoBleedingEdge/lib/mono/4.5/System.Drawing.dllをAssets/Pluginsフォルダにコピペ でおそらくいけるかなと... 試行錯誤したので抜けがあるかもです
1236

2021/01/13 15:47

System.Drawing.dllをAssets/Pluginsフォルダにコピペするだけでエラーはなくなりました。 しかし2つのスクリプトを空のオブジェクトにアタッチしようとすると Can't add script behaviour ImageComparator. The script needs to derive from MonoBehaviour! とエラーが出ます。ファイル名、クラス名も確認しましたが問題なく原因がわからない状態です。 libgdiplusのインストールとconfigファイルの編集も試しましたが変化はありませんでした。 System.Drawing.dllに関して、unityのバージョンが2019.4.16f1なのですが、どのバージョンのdllをコピペしたら良いでしょうか? 今は4.5にしてあります。
lazh

2021/01/13 18:16 編集

> Can't add script behaviour ImageComparator. The script needs to derive from MonoBehaviour! 原因はいろいろあるみたいですが今回のはImageComparatorがMonoBehaviourを継承していないのにアタッチしようとするから起こるエラーですね ただImageComparatorはアタッチしなくてもいいですそのままで大丈夫です! 一応こっちで動かしたProgram.csの内容を追記しておきます それを適当なオブジェクトにアタッチしてプレイしてみてください > System.Drawing.dll System.Drawing.dllはそれで大丈夫だと思います
1236

2021/01/14 07:04

Program.csを追記のものに書き換えたところアタッチできました。 しかし実行すると以下のエラーが出ます。 DllNotFoundException: /Users/bokken/build/output/Unity-Technologies/mono/external/buildscripts/add_to_build_results/monodistribution/lib/libgdiplus.dylib System.Drawing.GDIPlus..cctor () (at <04abc238e1e64b378e01047493bf1e72>:0) Rethrow as TypeInitializationException: The type initializer for 'System.Drawing.GDIPlus' threw an exception. System.Drawing.Bitmap..ctor (System.String filename, System.Boolean useIcm) (at <04abc238e1e64b378e01047493bf1e72>:0) System.Drawing.Bitmap..ctor (System.String filename) (at <04abc238e1e64b378e01047493bf1e72>:0) (wrapper remoting-invoke-with-check) System.Drawing.Bitmap..ctor(string) CSharpStudy.Image.ImageComparator.Compare (System.String bmp1Path, System.String bmp2Path, System.String path) (at Assets/script/ImageComparator.cs:26) CSharpStudy.Program.Start () (at Assets/script/Program.cs:19) configファイルを編集し再起動しても改善しませんでした。
lazh

2021/01/14 10:23

こちらでもconfigファイルから追加行を消してUnity再起動後実行すると同じエラーが出ました あとちょっと間違いがありました × (使っているバージョンの)Unity.App/Contents/MonoBleedingEdge/etc/config 〇 (使っているバージョンの)Unity.App/Contents/MonoBleedingEdge/etc/mono/config です ただたぶんお気づきになってると思うのでこれが原因ではないでしょう... 念のためconfigの中身のスクショと/Library/Frameworks/Mono.framework/Versions/XXXX/lib/フォルダのスクショ貼ってもらえますか?
1236

2021/01/14 11:30

スクショ画像を追記に貼りました。 よろしくお願いします。
lazh

2021/01/14 13:32

うーん自分がやったときと同じですね... 関係ないかもですが試行錯誤の途中でやったことは System.Drawingが使えないから代わりにSystem.Drawing.Commonを使うみたいな記事を見つけたので、Visual Studio for Mac(確か.Net Core,Android,iOSにチェックを入れたもの)をインストールしました それでNuGetでSystem.Drawing.Commonとruntime.osx.10.10-x64.CoreCompat.System.Drawingパッケージ入れてみたんですが、動かず そのあとlibgdiplusの流れをやってみたら動いた感じです Visual Studio for Mac https://visualstudio.microsoft.com/ja/downloads/ NuGet パッケージを追加する https://docs.microsoft.com/ja-jp/nuget/quickstart/install-and-use-a-package-in-visual-studio-mac#add-the-newtonsoftjson-nuget-package
1236

2021/01/20 05:27

本日Windowsで試したところ、正しく動作しました! 回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問