Imagesharp: Is there any equivalent for System.Drawing.Image.GetPixelFormatSize()?

Created on 26 Jun 2017  路  10Comments  路  Source: SixLabors/ImageSharp

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of ImageSharp
  • [x] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description


Just a short question, any help will be appreciated!

enhancement feature request

All 10 comments

You can use the standard sizeof(struct) operator to get the size of whichever pixel format you need.

e.g. to find the size of the Rgba32 pixel struct you can just use var size = sizeof(Rgba32);.

Great! But if I don't know pixel format of an image? Default format for Image.Load() is Rgba32, even if I load 8 bpp PNG

That's right, the pixel format is always whatever the TPixel is in Image<TPixel> how the underlying file format stores the data is ignored outside the decoder, the decoders convert it to the correct TPixel type you asked for..

Yes, You load the image as the given TPixel type.

  • Image.Load(stream) and Image<Rgba32>.Load both load the type as Rgba32
  • Image<Rgba24>.Load loads the type as Rgba24

and so on...

I got it, but I have to determine what exact BPP has particular image stored in Stream/byte[]/etc. How can I do that?

AFAIK, you can't do that currently. @JimBobSquarePants shouldn't we convert this into a feature request issue or open a new one?

Really this is a misuse of the issue tracker. The question should have been asked in Gitter.

It's a sensible requirement though so let's continue here and add the correct tags.

You can use the standard sizeof(struct) operator to get the size of whichever pixel format you need.
e.g. to find the size of the Rgba32 pixel struct you can just use var size = sizeof(Rgba32);

@tocsoft, unfortunately I can't use even sizeof(Rgba32) due to compile time error CS0233: 'Rgba32' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

@xakep139 If you can't use unsafe code in your library use System.Runtime.CompilerServices.Unsafe That has a SizeOf<T> method.

Fixed with #292

Was this page helpful?
0 / 5 - 0 ratings