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.