0

ソケット通信からimageasbyte[]を受信して​​いて、それを a で表示しようとしていpictureBoxます。コードを実行すると、次のようなメッセージ エラーのみが表示されます。"NullReferenceException"

例外を処理するキャッチはex1あり、私はチェックしましたが、そうでpicはないnullので、この例外が発生している理由を理解できません。

これは私のコードです:

try
{
    if (pictureBox1.InvokeRequired)
    {
        try
        {
            pic = imageEmp;
            addControlHandler c = new addControlHandler(addPic);
            this.Invoke(c);
        }
        catch (Exception exc) { MessageBox.Show(exc.Message); }
    }
    else
    {
        pictureBox1.Image = ByteToImage(imageEmp);
    }
}
catch (Exception ex1) 
{
    MessageBox.Show(ex1.Message);                
}

public void addPic()  //when invokeRequired == true
{
    pictureBox1.Image = ByteToImage(pic); 
}

変換するコードはbyte[]次のImageとおりです。

public Image ByteToImage(byte[] imageBytes)  //convert byte[] to image
{
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = new Bitmap(ms); 
    return image;
}

更新 1 : ハンスの回答に関して、私は彼に次の変更を加えます:

ByteToImage をハンスの答えに変更し、エラーがどこにあるかを確認するために、次の行をthis.Invoke(c)次の場所に追加しました。

if (c != null)
{
    try
    {
        this.Invoke(c);
    }
    catch (Exception e_c)
    {
        MessageBox.Show(e_c.Message, "exc from e_c");
    }
}

これは私に例外を与えます:NullReferenceException

助けてくれてありがとう!

更新 2:動作するようになりました。JPG の代わりに JPEG 画像を送信すると、表示されるようになりました。なぜこれが起こるのか分かりませんが、今は問題なく動作しています。

4

1 に答える 1

0

これはあなたが試すことができる例です私は今私自身の方法を使ってこれをテストしましたこれはうまくいくのであなたのコードを私のbtnStudentPic_Clickのコード行に置き換えてくださいこれがあなたのために働くかどうか私に知らせてください。

Compact Frameworkの場合は、代わりにこれを試してください

public static byte[] ReadAllBytes(string path)
{
byte[] buffer;

using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
int offset = 0;
int count = (int)fs.Length;
buffer = new byte[count];
while (count > 0)
{
int bytesRead = fs.Read(buffer, offset, count);
offset += bytesRead;
count -= bytesRead;
}
}

return buffer;
}

//以下のWindowsの例はCFには使用しません

    private void btnStudentPic_Click(object sender, EventArgs e)
    {
        Image picture = (Image)BrowseForPicture();
        this.picStudent.Image = picture;
        this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private Bitmap BrowseForPicture()
    {
       // Bitmap picture = null;

        try
        {
            if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK)
            {
                byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName);
                StudentPic = new Bitmap( this.fdlgStudentPic.FileName);
                StuInfo.StudentPic = imageBytes;
            }
            else
            {
                StudentPic = Properties.Resources.NoPhotoAvailable;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("That was not a picture.", "Browse for picture");
            StudentPic = this.BrowseForPicture();
        }

        return StudentPic;
    }
于 2012-08-01T18:27:51.697 に答える