简体   繁体   English

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

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

i have write this code for extracting bitplane1 of my image . 我已经写了这段代码来提取图像的bitplane1。 but i have exceptions. 但我有例外。 actually i get an image and convert it to a byte array so after i change this byte array i want to convert this new byte array to image ? 其实我得到一个图像并将其转换为字节数组,所以在更改此字节数组后,我想将此新的字节数组转换为图像吗? could you please give me some advice ? 你能给我一些建议吗? best regards (actually i want extract bitplane1 of my image) any suggestion ? 最好的问候(实际上我想提取图像的bitplane1)有什么建议吗?

my code is : 我的代码是:

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");
    }
    }
}

my exception is for this line : 我的例外是这一行:

Image myImage = Image.FromStream(ms); 图片myImage = Image.FromStream(ms);

System.ArgumentException was unhandled Parameter is not valid. 尚未处理System.ArgumentException。参数无效。

Well, I think the first exception you may get int this code is because of this: 好吧,我认为您可能会将此代码理解为第一个异常是因为:

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

Note that doesn't matters what happenes to the open file dialog, the byte[] x = File.ReadAllBytes(o.FileName); 请注意,打开文件对话框不会发生什么, byte[] x = File.ReadAllBytes(o.FileName); will always be executed once even if its value is null . 即使其值为null将始终执行一次。 I think you should first edit your code to sthg like this: 我认为您应该首先编辑代码以使其像这样:

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

Now the code will only be executed when the o object returns OK, which means a file was selected. 现在,仅当o对象返回OK时才执行代码,这意味着已选择文件。

The second thing is, that in your code there are a lot of places where an exception could be thrown, and in this case it's better and safer to use methods that are already exist. 第二件事是,在您的代码中有很多地方都可能引发异常,在这种情况下,使用已经存在的方法会更好,更安全。 Here's a method that calls one: 这是一个调用其中一个的方法:

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

This code does almost all the work that your code already contains. 此代码几乎完成了您的代码已包含的所有工作。

But there are cases when this code throws exceptions either, so the most safe way is to put all the stuff in a try-catch block: 但是在某些情况下,此代码也会引发异常,因此最安全的方法是将所有内容放入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);
        }
}

I inserted some methods to your code, so you can call them from anywhere, and don't have to do it inside only one event, but it does the same thing. 我在您的代码中插入了一些方法,因此您可以从任何地方调用它们,而不必在一个事件中进行操作,但是它执行相同的操作。

Hopefully helps a bit! 希望能有所帮助! :) :)

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

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