Getting type of RGB: sRGB or AdobeRGB in C#?

一个人想着一个人 提交于 2020-05-08 05:30:49

问题


I need to check if picture is sRGB or Adobe RGB in my WEBapplication. Is there a way to know exactly what RGB does the picture have?

UPDATE: Tried to Use Color.Context, but it's always null

code

Bitmap img = (Bitmap)image;
var imgPixel = img.GetPixel(0,0);
System.Windows.Media.Color colorOfPixel= System.Windows.Media.Color.FromArgb(imgPixel.A,imgPixel.R, imgPixel.G, imgPixel.B);
var context = colorOfPixel.ColorContext; //ColorContext is null

In System.Windows.Media also found PixelFormat and PixelFormats that can show what exact RGB type an image is. But still I can't find a way to get System.Windows.Media.PixelFormat of an img. How should I do this?


回答1:


You'll need to use a BitmapDecoder to get the frame from there, then check the color context:

BitmapDecoder bitmapDec = BitmapDecoder.Create(
   new Uri("mybitmap.jpg", UriKind.Relative),
   BitmapCreateOptions.None,
   BitmapCacheOption.Default);
BitmapFrame bmpFrame = bitmapDec.Frames[0];
ColorContext context = bmpFrame.ColorContexts[0];

Afterwards, you'd need to process the raw color profile (using context.OpenProfileStream()) to determine which profile it is.

If you want to write the profiles to disk to check them with an hex editor or something, you can use this code:

using(var fileStream = File.Create(@"myprofilename.icc"))
using (var st = context.OpenProfileStream())
{
  st.CopyTo(fileStream);
  fileStream.Flush(true);
  fileStream.Close();
}

Using that method, I've extracted the raw data from both sRGB (link) and AdobeRGB (link) if you want to check them. There are magic strings and IDs at the start if you want to check but I really don't know them or know where to find a table for the common ones (the embedded profiles could be infinite, not limited to AdobeRGB and sRGB).

Also, one image might have more than one color profile.

With this method, if ColorContexts is empty, then the image doesn't have any profile.




回答2:


Color.ColorContext Property MSDN: https://msdn.microsoft.com/en-us/library/System.Windows.Media.Color_properties(v=vs.110).aspx




回答3:


You might use System.Drawing.Image.PropertyItems. The property "PropertyTagICCProfile" (Id=34675=0x8773) is populated with the icc profile of the image, even if it is embedded in image data and not in exif data (or there is no profile embedded, but the image is labeled as AdobeRGB in exif: InteroperabilityIndex="R03").

byte[] iccProfile = null;
try {
    System.Drawing.Bitmap myImage = new Bitmap("Image.jpg");
    iccProfile = myImage.GetPropertyItem(34675).Value;
} catch (Exception) {
    //...
}


来源:https://stackoverflow.com/questions/28733835/getting-type-of-rgb-srgb-or-adobergb-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!