簡體   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