0

I'm trying to load in a 16bpp grayscale image (.tiff file) as a bitmap, but I have to process and trim this image. To do that, I thought I'd need to convert it into a byte array first, and then delete elements in the array accordingly (among other things).

But after converting it, I'm getting that the length of the array is less than the width x height of the original image in pixels?

 private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            byte[] valsArray;  // for testing

            openfile.Title = "Open Image";
            openfile.Filter = "Image File|*.bmp; *.gif; *.jpg; *.jpeg; *.png;*.tif;*.tiff;";

            if (openfile.ShowDialog() == true)
            {
                //using (var photo = new Bitmap(_filePath = openfile.FileName))
                using (Stream stream = new FileStream(openfile.FileName,FileMode.Open,FileAccess.Read))
                {
                    _photo = new Bitmap(stream);
                    imgSrc.Width = _photo.Width;
                    imgSrc.Height = _photo.Height;
                    imgSrc.Source = BitmapToImageSource(_photo);
                }
            }
        }


private BitmapImage BitmapToImageSource(Bitmap bitmap)
        {
            BitmapImage bitmapimage = new BitmapImage();
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Tiff);
                convertedBrightnessVals = memory.ToArray(); // testing code to store data into byte array
                memory.Position = 0;
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();
            }

            return bitmapimage;
        }

The image is 816x624 pixels, but I end up getting the byte array is 466422 bytes long, which is less than 816x624.

Also - the properties say that this image is 1dpi for both horizontal and vertical resolution. I'm not sure if that affects this or how to change it if it does.

Denis Hu
  • 3
  • 2
  • Looks like you need an array of *pixels*, but instead you are writing the `tiff` format bytes (i.e. encoding the image to the tiff and then write that tiff data, not pixels). You can refer to the link in the previous comment to get an array of pixels. You may also want to consider using of `Magick.NET`, 'ImageSharp' or any other image processing library to work with your images instead of working with pixels by hand. – Serg Jul 10 '23 at 21:37
  • You really want to use a library to do this. Image formats are tricky, and it's best to let library authors worry about those tricks and test their code to make sure all the tricky scenarios are covered – Flydog57 Jul 10 '23 at 22:04
  • My god, this is why SO has such a bad reputation... a genuine question nuked as duplicate while the linked question is completely unrelated to the specifics in this question, and all answers use horribly slow methods. Perhaps the answer to this is already on SO, yes, but it certainly isn't on _that_ question. – Nyerguds Jul 28 '23 at 07:45
  • That said, the simplest way to trim an image is not to edit bytes but simply to paint the correct portion of the loaded image onto a new image, which can be done with simple drawing methods. Do note that the .net framework does not contain a 16bpp grayscale format; any operations performed on such an image would reduce the grayscale accuracy to a 0-255 range. – Nyerguds Jul 28 '23 at 07:47
  • As for your byte array, it is a tiff image, not raw pixel bytes. TIFF supports compression, so I'm assuming the framework is applying compression when saving it. If you want the raw image bytes, look around SO for methods that use `LockBits` and `Marshal.Copy`. But as I said, since you start by loading it as `Bitmap`, its 16bpp accuracy is definitely already gone. _(And, on the note of that `Bitmap`, it is an `IDisposable` and should be disposed after use to not get memory leaks; ideally by putting it in a `using` block)_ – Nyerguds Jul 28 '23 at 08:06

0 Answers0