ページ 11

C# アイコンの読み込みでエラーが起きる

Posted: 2016年11月03日(木) 20:27
by dic
[1] 質問文
 [1.1] 自分が今行いたい事は何か
  C#でアイコンを読み込み、表示させたい

 [1.2] どのように取り組んだか(プログラムコードがある場合記載)

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace リスト6_14_アイコンの描画
{
    public partial class Form1 : Form
    {
        private Icon icon;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (icon == null)
                icon = new Icon("test.ico");
            e.Graphics.DrawIcon(icon, 0, 0);
            e.Graphics.DrawIcon(icon, new Rectangle(icon.Width, 0, 200, 200));
        }
        public Form1()
        {
            InitializeComponent();
        }
    }
}
 [1.3] どのようなエラーやトラブルで困っているか(エラーメッセージが解る場合は記載)
  ソースコードの20行目で停止
  ArgumentExceptionはハンドルされませんでした。
  引数 picture は Icon として使用できるピクチャでなければなりません。
  というダイアログボックスが出る

 [1.4] 今何がわからないのか、知りたいのか
  アイコンのファイルは正常なアイコンの情報を持っているか
  ソースコードに問題点はないのか

[2] 環境  
 [2.1] OS : Windows, Linux等々
  OS:Windows7 32ビット HomeEdition

 [2.2] コンパイラ名 : VC++ 2008EE, Borand C++, gcc等々
  コンパイラ:Visual C# 2010 Express

[3] その他
 ・どの程度C言語を理解しているか
  オブジェクト指向を理解できる、作成できる

 ・ライブラリを使っている場合は何を使っているか
  今回は.NETを使っている(?)

Re: C# アイコンの読み込みでエラーが起きる

Posted: 2016年11月03日(木) 20:43
by あんどーなつ
Visual Studio 2015 Professional で試験しました。
自前の.icoファイルは読めましたが、dicさんのは読めませんでした。

Form2.cs

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form2 : Form
    {
        private Icon icon;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (icon == null)
            {
                OpenFileDialog d = new OpenFileDialog();
                d.ShowDialog();
                icon = new Icon(d.FileName);
            }
            e.Graphics.DrawIcon(icon, 0, 0);
            e.Graphics.DrawIcon(icon, new Rectangle(icon.Width, 0, 200, 200));
        }
        public Form2()
        {
        }
    }
}
Program.cs

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test1
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }
}

Re: C# アイコンの読み込みでエラーが起きる

Posted: 2016年11月03日(木) 20:46
by あんどーなつ
ちなみに私はアイコンウィザードというソフトで.icoファイルを作成しています。

http://www.vector.co.jp/soft/winnt/amus ... 1476170939

Re: C# アイコンの読み込みでエラーが起きる

Posted: 2016年11月03日(木) 22:15
by dic
訂正

[2] 環境  
 [2.1] OS : Windows, Linux等々
  OS:Windows7 32ビット HomeEdition

  OS:Windows7 32ビット Professional Edtionでした。すいません。



あんどーなつ様の指定してくれたソフトは私の環境では動作しなかったので、
ネットからアイコンを拾ってきて入れ替えたところ、アイコンが表示されました。
私のアイコンのファイルがビットマップのままではダメなようでした。
(拡張子をicoにしただけのビットマップ)

解決しました。ありがとうございました。