繁体   English   中英

如何将新的字节数组转换为图像

[英]How Can I Convert My new Byte Array To an Image

我已经写了这段代码来提取图像的bitplane1。 但我有例外。 其实我得到一个图像并将其转换为字节数组,所以在更改此字节数组后,我想将此新的字节数组转换为图像吗? 你能给我一些建议吗? 最好的问候(实际上我想提取图像的bitplane1)有什么建议吗?

我的代码是:

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


namespace bitplane
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)

    {
      //  Image grayImage;
        OpenFileDialog o = new OpenFileDialog();
        o.ShowDialog();

        byte[] x = File.ReadAllBytes(o.FileName);

        byte maskbyte1 = 2;
        int [] newpix= new int [x.Length];
    for (int i = 0; i < x.Length; i++)
       {


           newpix[i] = x[i] & maskbyte1;

           string px=newpix[i].ToString();


          x[i] = Convert.ToByte(px);

    }
        MemoryStream ms = new MemoryStream(x);
        Image myImage = Image.FromStream(ms);

    myImage.Save(@"C:\Users\Public\Pictures\Sample Pictures\New folder\fgh.jpg");
    }
    }
}

我的例外是这一行:

图片myImage = Image.FromStream(ms);

尚未处理System.ArgumentException。参数无效。

好吧,我认为您可能会将此代码理解为第一个异常是因为:

o.ShowDialog();
byte[] x = File.ReadAllBytes(o.FileName);

请注意,打开文件对话框不会发生什么, byte[] x = File.ReadAllBytes(o.FileName); 即使其值为null将始终执行一次。 我认为您应该首先编辑代码以使其像这样:

if (o.ShowDialog() == DialogResult.OK)
{
    byte[] x = File.ReadAllBytes(o.FileName);
    //...  and all other codes
}

现在,仅当o对象返回OK时才执行代码,这意味着已选择文件。

第二件事是,在您的代码中有很多地方都可能引发异常,在这种情况下,使用已经存在的方法会更好,更安全。 这是一个调用其中一个的方法:

public Image ConvertByteArrayToImage(byte[] bytes)
{
    System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
    return Image.FromStream(stream);
}

此代码几乎完成了您的代码已包含的所有工作。

但是在某些情况下,此代码也会引发异常,因此最安全的方法是将所有内容放入try-catch块中:

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


namespace bitplane
{
    public partial class Form1 : Form
    {
        public byte[] x;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (o.ShowDialog() == DialogResult.OK)
                {
                    OpenImage(o.FileName);
                    SaveImage(ConvertByteArrayToImage(x), @"C:\Users\Public\Pictures\Sample Pictures\New folder\fgh.jpg");
                }
            }
            catch 
            {
                MessageBox.Show("error");
            }
        }

        public void OpenImage(string path)
        {
            x = File.ReadAllBytes(path);
        }

        public void SaveImage(Image image, string path)
        {
            image.Save(path);
        }

        public Image ConvertByteArrayToImage(byte[] bytes)
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
            return Image.FromStream(stream);
        }
}

我在您的代码中插入了一些方法,因此您可以从任何地方调用它们,而不必在一个事件中进行操作,但是它执行相同的操作。

希望能有所帮助! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM