如何创建包含真实图像的字节数组?

时间:2014-01-07 20:41:56

标签: c# bytearray image-conversion

请参阅下面的代码。

我想创建一个Byte数组,其中包含可以转换为真实图像的数据。当我尝试运行此代码时,我得到一个argumentException。在For循环中我需要做什么才能创建一个保存图像数据的合法Byte数组?我不想使用真实图像并将其转换为字节数组,我想创建一个随机数字形式的图像。

    Random Rnd = new Random();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Byte[] ByteArray = new Byte[1000];

        for (int i = 0; i < 1000; i++)
        {
            ByteArray[i] = Convert.ToByte(Rnd.Next(9));                
        }
        ImageConverter Convertor = new ImageConverter();
        BitmapImage image = (BitmapImage)Convertor.ConvertFrom(ByteArray);
        MyImage.Source = image;
    }

请注意,我不想使用WinForms类型或库,如system.drawing / bitmap - 我只想使用WPF技术。

4 个答案:

答案 0 :(得分:4)

这是您正在寻找的解决方案,仅使用WPF技术。

请注意,步幅参数计算中使用的常量值16直接来自我使用的是16位像素格式。

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Random rnd = new Random();

        Byte[] ByteArray = new Byte[(int)MyImage.Width * (int)MyImage.Height * 3];

        rnd.NextBytes(ByteArray);

        var image = BitmapSource.Create((int) MyImage.Width, (int) MyImage.Height, 72, 72,
            PixelFormats.Bgr565, null, ByteArray, (4*((int)MyImage.Width * 16 + 31)/32));

        MyImage.Source = image;
    }

答案 1 :(得分:0)

我不确定Converter.ConvertFrom是如何运作的,但我更喜欢使用Bitmap.LockBits()Marshal.Copy()的较低级别方式来制作我的位图。

请参阅此方法:

    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;

    static Bitmap CreateRandomBitmap(Size size)
    {
        // Create a new bitmap for the size requested.
        var bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);

        // Lock the entire bitmap for write-only acccess.
        var rect = new Rectangle(0, 0, size.Width, size.Height);
        var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, bitmap.PixelFormat);

        // Calculate the number of bytes required and allocate them.
        var numberOfBytes = bitmapData.Stride * size.Height;
        var bitmapBytes = new byte[numberOfBytes];

        // Fill the bitmap bytes with random data.
        var random = new Random();
        for (int x = 0; x < size.Width; x++)
        {
            for (int y = 0; y < size.Height; y++)
            {
                // Get the index of the byte for this pixel (x/y).
                var i = ((y * size.Width) + x) * 4; // 32bpp

                // Generate the next random pixel color value.
                var value = (byte)random.Next(9);

                bitmapBytes[i] = value;         // BLUE
                bitmapBytes[i + 1] = value;     // GREEN
                bitmapBytes[i + 2] = value;     // RED
                bitmapBytes[i + 3] = 0xFF;      // ALPHA
            }
        }

        // Copy the randomized bits to the bitmap pointer.
        var ptr = bitmapData.Scan0;
        Marshal.Copy(bitmapBytes, 0, ptr, numberOfBytes);

        // Unlock the bitmap, we're all done.
        bitmap.UnlockBits(bitmapData);
        return bitmap;
    }

然后你可以这样做:

    public void Run()
    {
        using(var bitmap = CreateRandomBitmap(new Size(64, 64)))
        {
            bitmap.Save("random.png", ImageFormat.Png);
        }
    }

答案 2 :(得分:0)

这可能对你有所帮助:

    private static Bitmap GenBitmap(int width, int height) {

        int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)
        Random rnd = new Random();

        int imageByteSize = width * height * ch;

        byte[] imageData = new byte[imageByteSize]; //your image data buffer
        rnd.NextBytes(imageData);       //Fill with random bytes;

        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);

        BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
        IntPtr pNative = bmData.Scan0;
        Marshal.Copy(imageData, 0, pNative, imageByteSize);
        bitmap.UnlockBits(bmData);
        return bitmap;

    }

答案 3 :(得分:-3)

您不能使用随机字节来创建图像,因为每种类型的图像(bmp,jpeg,png)都是使用某种格式构建的。代码不知道如何解释随机字节。

http://en.wikipedia.org/wiki/Image_file_formats