0

I have code which I use to crop an image to a specified size. It does the job apart from the fact that, when I crop a bitonal image, it converts it to 24-bit colour which I don't want. Here's a snippet from the code ...

using (var Bmp = new Bitmap(FileImage.Width, Height))
{
    using (var Graphic = Graphics.FromImage(Bmp))
    {
        var MemoryStreamTemp = new MemoryStream();
        Graphic.DrawImage(FileImage, new Rectangle(0, 0, FileImage.Width, Height), x, CheckY, FileImage.Width, Height, GraphicsUnit.Pixel);
        Bmp.Save(MemoryStreamTemp, ImageFormat.Tiff);

When bmp is created, it defaults to 24-bit colour. I realise that I can specify 1bpp indexed but that causes an exception when the Graphic object is instantiated.

I could convert the bitmap back to bitonal after it is cropped but that seems an unnecessary step that I would prefer to avoid.

Incidentally, I have a similar problem with the file's compression and resolution but that's for another day.

JockyMac
  • 9
  • 1
  • `I could convert the bitmap back to bitonal after it is cropped but that seems an unnecessary step` - unfortunately, [it's not](https://stackoverflow.com/q/15628385/11683), but it might be that the situation with `Encoder.ColorDepth` is better for Tiff. – GSerg Sep 25 '17 at 13:34
  • 1
    _new Bitmap(FileImage.Width, Height)_ If you don't want to create a default pixelformat, do specify the format you want right there! – TaW Sep 25 '17 at 19:27
  • Editing indexed images as indexed images is a bit of a mess... you basically have to handle them as byte array. And it's advised to convert them to 1 byte per pixel for ease of processing, unless you want to mess around with bitshifting all the time. I got code for all of this, but... it's almost its own library by now. – Nyerguds Mar 05 '18 at 13:29

0 Answers0