This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

How do I access a pixels color without loading a bitmap?

I want to crop very large images according to their colors in each corner. I know of the Transforms.AutoCrop which just needs a color as argument. The problem is I only know the color at runtime. Both approaches from Accessing Pixel Data need to load the bitmap, which is not possible due to the size of the image.
I came up with:
  • Cropping the image to the pixel I want the color of with a pipeline
  • Load the bitmap.
  • Use GetPixel(0,0)

var memoryStream = new MemoryStream();

using (var imageReader = ImageReader.Create(@"..\Input\largeImage.tif"))
using (var crop = new Crop(0, 0, 1, 1))
using (var writer = new TiffWriter(memoryStream))
{
     Pipeline.Run(imageReader + crop + writer);
}

var resultReader = ImageReader.Create(memoryStream);
var color = resultReader.Frames[0].GetBitmap().GetPixel(0, 0);

But this is an extra pipeline I need to use and it just feels wrong. Furthermore it may not even be possible to start this Pipeline, because the reader is already used in another one which is under construction (the one which uses the autocrop).

Are there any other waysto access a pixels data without loading the entire bitmap?
You need to read an image to get its pixels. So you need to open, close, and reopen an image reader. However, you can write a result directly to Bitmap:

Color color;

using (var imageReader = ImageReader.Create(@"..\..\..\largeImage.tif"))
using (var crop = new Crop(0, 0, 1, 1))
using (var bitmap = new Bitmap())
{
	Pipeline.Run(imageReader + crop + bitmap);

	color = bitmap.GetPixel(0, 0);
}

Console.WriteLine(color);


If you need to get all corner pixels then you can you use a single pipeline:

Color topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;

using (var imageReader = ImageReader.Create(@"..\..\..\largeImage.tif"))
using (var topLeftCrop = new Crop(0, 0, 1, 1))
using (var topLeftBitmap = new Bitmap())
using (var topRightCrop = new Crop(imageReader.Width - 1, 0, 1, 1))
using (var topRightBitmap = new Bitmap())
using (var bottomLeftCrop = new Crop(0, imageReader.Height - 1, 1, 1))
using (var bottomLeftBitmap = new Bitmap())
using (var bottomRightCrop = new Crop(imageReader.Width - 1, imageReader.Height - 1, 1, 1))
using (var bottomRightBitmap = new Bitmap())
{
	imageReader.Receivers.Add(topLeftCrop);
	topLeftCrop.Receivers.Add(topLeftBitmap);

	imageReader.Receivers.Add(topRightCrop);
	topRightCrop.Receivers.Add(topRightBitmap);

	imageReader.Receivers.Add(bottomLeftCrop);
	bottomLeftCrop.Receivers.Add(bottomLeftBitmap);

	imageReader.Receivers.Add(bottomRightCrop);
	bottomRightCrop.Receivers.Add(bottomRightBitmap);

	imageReader.RunPipeline();
			
	topLeftColor = topLeftBitmap.GetPixel(0, 0);
	topRightColor = topRightBitmap.GetPixel(0, 0);
	bottomLeftColor = bottomLeftBitmap.GetPixel(0, 0);
	bottomRightColor = bottomRightBitmap.GetPixel(0, 0);
}

Console.WriteLine(topLeftColor);
Console.WriteLine(topRightColor);
Console.WriteLine(bottomLeftColor);
Console.WriteLine(bottomRightColor);
I have to say I am not completely satisfied to have to do such a thing on my own and in such an eloborate fashion. However, I used your bitmapversion to implement an extension method to imagereader.

Thank you for your reply.