Search code examples
c#wpfimagemagickmagick.net

How can i make byte array from NEF or another RAW Format and edit it?


im trying to make simple photo editor (for adjusting brightness, contrast, saturation etc...) in WPF and im using the Magick.NET which contains ToByteArray method but the problem is i can't make ByteArray with this method because i'm getting exception because it doesn't wanna make ByteArray from NEF format for some reason...

My first idea, was to take the NEF format convert it to temporary TIFF File which can be converted with this specific method to ByteArray and that file can be converted to bytearray, but i think is very inconvenient and not much smart.

Second thing is when i have the WritableBitmap. How can i make the image from it for Magick.NET to edit it? Should i make another MagickImage Instance which is created with already created ByteArray and edit that image?

    public void ImageSelectBtn_Click(object sender, RoutedEventArgs e)
    {

        OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == true)
        {
            using (var imageRaw = new MagickImage(dialog.FileName))
            {
                imageRaw.Write("D:/Coding/C#/New/SoftlightWPF/SoftlightWPF/Resources/temp.tiff");
                MagickImage image = new MagickImage();
                image = new MagickImage("D:/Coding/C#/New/SoftlightWPF/SoftlightWPF/Resources/temp.tiff");
                byte[] ImageBytes = image.ToByteArray();
                Render(ImageBytes);
            }

        }
    }
    private void Render(byte[] BytesData)
    {
        using (var ms = new MemoryStream(BytesData))
        {

            BitmapImage ImageBitmapSource = new BitmapImage(); //Image
            ImageBitmapSource.BeginInit();
            ImageBitmapSource.CacheOption = BitmapCacheOption.OnLoad;
            ImageBitmapSource.StreamSource = ms;
            ImageBitmapSource.EndInit();

            WriteableBitmap ImageWritableBitmap = new WriteableBitmap(ImageBitmapSource);
            this.ImageField.Source = ImageWritableBitmap;
        }
    }

Solution

  • There is already a library that can help you with this: https://www.nuget.org/packages/Magick.NET.SystemWindowsMedia/. Add it to your project and then do image.ToBitmapSource() instead.