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

C# 如何将新字节数组转换为图像,c#,image-processing,C#,Image Processing,我写了这段代码来提取我的图像的bitplane1。但我也有例外。 实际上,我得到一个图像并将其转换为字节数组,所以在我更改这个字节数组之后,我想将这个新的字节数组转换为图像?你能给我一些建议吗?顺致敬意, (实际上我想提取图像的bitplane1) 有什么建议吗 我的代码是: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Dr

我写了这段代码来提取我的图像的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");
    }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用系统集合;
命名空间位平面
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//图像灰度图像;
OpenFileDialog o=新建OpenFileDialog();
o、 ShowDialog();
byte[]x=File.ReadAllBytes(o.FileName);
字节maskbyte1=2;
int[]newpix=新int[x.Length];
对于(int i=0;i

我的例外是这一行:

Image myImage=Image.FromStream(毫秒)

System.ArgumentException未处理
参数无效。

好吧,我想您可能会得到int的第一个例外是因为以下原因:

o.ShowDialog();
byte[] x = File.ReadAllBytes(o.FileName);
请注意,“打开文件”对话框中出现的内容并不重要,
byte[]x=file.ReadAllBytes(o.FileName)将始终执行一次,即使其值为
null
。我认为您应该首先将代码编辑为sthg,如下所示:

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);
        }
}
我在代码中插入了一些方法,因此您可以从任何地方调用它们,而不必只在一个事件中执行,但它也执行相同的操作


希望有点帮助!:)

你说你有例外,你有什么例外?它们发生在哪一行?谢谢亲爱的@Richtenzorg。但我的主要错误是将修改后的字节数组转换为图像。您有什么建议吗?@user3384232您的问题中是否存在异常(System.ArgumentException;参数无效)?因为字节和图像都会抛出很多异常,即使它们看起来很简单。