【问题标题】:C# Read Byte [] to ImageC# 读取字节 [] 到图像
【发布时间】:2014-08-03 12:26:27
【问题描述】:

我有一个添加图片的应用程序,这些图片会自动转换为二进制文件并存储在单个文件中。我怎样才能保存几个图像,我在一个 XML 文件中保存每组引用的图像字节的开始和大小。但它有几个以字节为单位的图像,每当我尝试选择一组不同的字节时,只是打开同一个图像。我希望您能帮助我解决此问题并打开不同的图像。

代码

//添加图片

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

【问题讨论】:

  • 这还能编译吗?你打电话给getImageFromByte(),但没有传递任何参数。
  • 我忘了放在那里,但应用程序有
  • 然后编辑你的问题。当您的问题充满误导性的内容时,人们很难给出好的答案。

标签: c# image byte converter


【解决方案1】:

FileStream.Read 的 offset 参数是缓冲区中您希望放置数据的起始偏移量。如果要从流中的偏移量读取,则必须 Seek 到该位置。我认为您想要的内容如下,尽管我不完全确定 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;
}

【讨论】:

  • 感谢您的回复。显然是正确的,但我尝试以两种不同的方式调用代码:getImageFromByte (0,12161);和 getImageFromByte (12162,19905);理论上是打开两张不同的图片,但是打开的是同一张图片
  • @LucasGuitar:这两个例子你都试过了吗?您是否单步执行了代码并调查了它在做什么?另外,您确定您的保存代码正在正确写入文件吗?
  • 我认为错误是创建二进制文件的时间
  • 我明白了!我会把完整的代码放在我原来的问题中。
【解决方案2】:

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

创建一个类

[serializable]
myImage
{
   Byte[] ImageData;
}

Add each image to a List
List<myImage>

使用序列化直接写入文件 使用反序列化直接读取 List

【讨论】:

  • 我听不懂
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
相关资源
最近更新 更多