2015-03-31 2 views
0

Мне нужно сделать снимок экрана части экрана и проверить, соответствует ли скриншот изображению в pictureBox2.Значение не может быть нулевым. Имя параметра: encoder

Это линия, которая не работает:

pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat); 

Ошибка

Значение не может быть пустым. Имя
Параметр: кодировщик

enter image description here

Мой код приведен ниже:

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; 
using System.Drawing.Imaging; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, 
      SystemInformation.VirtualScreen.Height, 
      PixelFormat.Format32bppArgb); 
     public Form1() 
     { 
      InitializeComponent(); 

     } 



     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, 
       SystemInformation.VirtualScreen.Height, 
       PixelFormat.Format32bppArgb); 
      Graphics screenGraph = Graphics.FromImage(screenshot); 
      screenGraph.CopyFromScreen(
       SystemInformation.VirtualScreen.X + 1080, 
       SystemInformation.VirtualScreen.Y + 100, 
       0, 
       0, 
       new Size(190, 480),//SystemInformation.VirtualScreen.Size, 
       CopyPixelOperation.SourceCopy); 
      pictureBox1.Image = screenshot; 
      MemoryStream ms = new MemoryStream(); 
      pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat); 
      String image_one = Convert.ToBase64String(ms.ToArray()); 
      ms.Position = 0; 
      pictureBox2.Image.Save(ms, pictureBox2.Image.RawFormat); 
      String image_three = Convert.ToBase64String(ms.ToArray()); 
      ms.Position = 0; 
      ms.Close(); 
      if (image_one.Equals(image_three)) 
       textBox1.Text = "SAME"; 
      else 
       textBox1.Text = "DIFFERENT"; 
     } 

    } 
} 

Что я могу сделать?

+0

pictureBox1.Image <- проверьте, не является ли это NULL – MajkeloDev

+0

@MajkeloDev, что приведет к тому, что NRE не будет аргументом ArgumentNullException. – HimBromBeere

+1

Отлаживайте код и смотрите, что именно равно null, не должно быть проблемой. Также вы можете захотеть упаковать MemoryStream внутри инструкции using: using (MemoryStream ms = new MemoryStream()) {} в конце закрывающей скобки MemoryStream автоматически удаляется. –

ответ

1

Ваше изображение существует в памяти и поэтому не имеет кодировщика для RawFormat. Укажите формат, например, System.Drawing.Imaging.ImageFormat.Png или что-то еще.

 Смежные вопросы

  • Нет связанных вопросов^_^