Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C将字节[]读取到图像_C#_Image_Byte_Converter - Fatal编程技术网

C# C将字节[]读取到图像

C# C将字节[]读取到图像,c#,image,byte,converter,C#,Image,Byte,Converter,我有一个应用程序,我正在添加图片,这些图片会自动转换为二进制文件并存储在单个文件中。如何保存多个图像,我在一个XML文件中保留每一组refente的开始和大小为一个图像字节。但是它有几个以字节为单位的图像,每当我试图选择一组不同的字节来打开同一个图像时。我希望您的帮助能够修复此问题并打开不同的图像 代码 //添加图像 private void btAddImage_Click(object sender, RoutedEventArgs e) { Open

我有一个应用程序,我正在添加图片,这些图片会自动转换为二进制文件并存储在单个文件中。如何保存多个图像,我在一个XML文件中保留每一组refente的开始和大小为一个图像字节。但是它有几个以字节为单位的图像,每当我试图选择一组不同的字节来打开同一个图像时。我希望您的帮助能够修复此问题并打开不同的图像

代码

//添加图像

private void btAddImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Selecione a Imagem";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                "Portable Network Graphic (*.png)|*.png";

            if (op.ShowDialog() == true)
            {
                imgPatch.Source = new BitmapImage(new Uri(op.FileName));
                txtName.Focus();
            }
        }


//Convert Image

 private void btConvertImage_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show("Preencha o Nome", "Error");
            }

            else
            {
                save(ConvertFileToByteArray(op.FileName), txtName.Text);
            }
        }

//Image to Byte Array

private static byte[] ConvertFileToByteArray(String FilePath)
        {
            return File.ReadAllBytes(FilePath);
        }


//Save Binary File and XML File

public void save(byte[] img, string nome)
        {
            FileStream f;
            long ini, fin = img.Length; 

            if (!File.Exists("Escudos.bcf"))
            {
                f = new FileStream("Escudos.bcf", FileMode.Create);
                ini = 0;
            }

            else
            {
                f = new FileStream("Escudos.bcf", FileMode.Append);
                ini = f.Length + 1;
                bin = new TestBinarySegment();

            }

            bin.LoadAddSave("Escudos.xml", "Brasileiro", nome, ini, fin);



            BinaryWriter b = new BinaryWriter(f);
            b.Write(img);
            b.Close();
            f.Dispose();



        }


//Load Image from Byte
private void btLoad_Click(object sender, RoutedEventArgs e)
        {
            getImageFromByte();
        }


//Byte to Image
public void getImageFromByte(int start, int length)
        {
            using (FileStream fs = new FileStream("Escudos.bcf", FileMode.Open))
            {
                byte[] iba = new byte[fs.Length+1];
                fs.Read(iba, start, length);
                Image image = new Image();
                image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None,
                                          BitmapCacheOption.OnLoad);

                imgPatch2.Source = image.Source;
            }
        }
要设置的偏移量参数是缓冲区中要放置数据的起始偏移量。如果要从流中的偏移量读取,则必须搜索到该位置。我认为您需要的是下面的内容,尽管我不能完全确定BitmapFrame.Create在文件中包含的数据超过它试图读取的图像时会执行什么操作

fs.Seek(start, SeekOrigin.Begin);
Image image = new Image();
image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
将文件指针移动到正确的起始位置

我删除了字节数组,因为您似乎没有将其用于任何有建设性的用途

如果这不起作用,则必须将数据读入字节数组,创建MemoryStream,然后从中创建位图:

byte[] ida = new byte[length];
using (FileStream fs = File.OpenRead("Escudos.bcf"))
{
    fs.Seek(start, SeekOrigin.Begin);
    fs.Read(ida, 0, length);
}
using (MemoryStream ms = new MemoryStream(ida))
{
    Image image = new Image();
    image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None,
        BitmapCacheOption.OnLoad);
    imgPatch2.Source = image.Source;
}

最简单的方法是使用序列化和反序列化

上课

[serializable]
myImage
{
   Byte[] ImageData;
}

Add each image to a List
List<myImage>
使用序列化直接写入文件
使用反序列化直接读取列表

这是否编译?您调用getImageFromByte,但未传递任何参数。我忘记在其中输入参数,但应用程序已编辑您的问题。当你的问题充满了误导性的东西时,人们很难给出好的答案。谢谢你的回答。显然是正确的,但我尝试用两种不同的方式调用代码:getImageFromByte 012161;从字节1216219905获取图像;从理论上讲,是打开两个不同的图像,但打开相同的图像image@LucasGuitar:两个例子你都试过了吗?您是否单步执行代码并调查它在做什么?另外,您确定您的保存代码正确地写入了文件吗?我想是时候创建我得到的二进制文件了!我将把完整的代码放在我的原始问题中。我不明白