获取RGB类型:在C#中是sRGB还是AdobeRGB? [英] Getting type of RGB: sRGB or AdobeRGB in C#?

查看:420
本文介绍了获取RGB类型:在C#中是sRGB还是AdobeRGB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查WEB应用程序中图片是sRGB还是Adobe RGB.有没有办法确切知道图片具有什么RGB?

更新: 尝试使用Color.Context,但始终为空

代码

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

在System.Windows.Media中,还发现了PixelFormat和PixelFormats,它们可以显示图像的确切RGB类型. 但是我仍然找不到获取img的System.Windows.Media.PixelFormat的方法. 我该怎么办?

解决方案

您将需要使用BitmapDecoder从此处获取框架,然后检查颜色上下文:

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

然后,您需要处理原始颜色配置文件(使用context.OpenProfileStream())以确定它是哪个配置文件.

如果要将配置文件写入磁盘以使用十六进制编辑器或其他工具进行检查,则可以使用以下代码:

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

使用该方法,我从两个sRGB中提取了原始数据(链接)和AdobeRGB(链接)来检查它们.如果要检查的话,一开始会有魔术字符串和ID,但我真的不知道它们,也不知道在哪里可以找到普通的字符串(嵌入的配置文件可以是无限的,不仅限于AdobeRGB和sRGB)./p>

此外,一幅图像可能具有多个颜色配置文件.

使用此方法,如果ColorContexts为空,则图像没有任何配置文件.

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?

解决方案

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.

这篇关于获取RGB类型:在C#中是sRGB还是AdobeRGB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆