C# 如何检测 jpeg 是否包含 cmyk 颜色配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/446834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect if a jpeg contains cmyk color profile?
提问by
I have some code that scales the images users upload. It works perfectly in most situations, but sometimes they upload jpegs that contain cmyk information.
我有一些代码可以缩放用户上传的图像。它在大多数情况下都能完美运行,但有时他们会上传包含 cmyk 信息的 jpeg。
After googling a bit, it seems like jpegs with cmyk values isn't valid, but since they work in windows, the users assume it's a problem with my application, so I need to be able to handle those situations. The questions:
谷歌搜索了一下之后,似乎带有 cmyk 值的 jpegs 无效,但由于它们在 Windows 中工作,用户认为这是我的应用程序的问题,所以我需要能够处理这些情况。问题:
How do I detect if the jpeg contains cmyk information?(it would allow me to inform the user why it doesn't work).
我如何检测 jpeg 是否包含 cmyk 信息?(它允许我通知用户为什么它不起作用)。
How can I convert it to a normal jpeg?
如何将其转换为普通的 jpeg?
回答by Ismael
Jpeg is a standard which support any number of encoded planes (they are compressed independently of each other) inside a bitstream, so a jpeg with a cmyk profile is perfectly valid. Most jpeg files are encoded using a jfif container (http://en.wikipedia.org/wiki/JFIF) which originally only supported grayscale images, YCbCr, or sRGB, but it is extensible and Adobe have a custom tag to support cmyk profiles.
Jpeg 是一种标准,它支持比特流内任意数量的编码平面(它们彼此独立压缩),因此具有 cmyk 配置文件的 jpeg 是完全有效的。大多数 jpeg 文件使用 jfif 容器 ( http://en.wikipedia.org/wiki/JFIF)进行编码,该容器最初仅支持灰度图像、YCbCr 或 sRGB,但它是可扩展的,并且 Adobe 有一个自定义标签来支持 cmyk 配置文件.
Take a look at this link for a workaround http://www.jroller.com/greenhorn/entry/adobe_photoshop_and_jpeg_cmyk, it is in java but you can port it easily to c#.
看看这个链接的解决方法 http://www.jroller.com/greenhorn/entry/adobe_photoshop_and_jpeg_cmyk,它是在 java 中,但你可以轻松地将它移植到 c#。
回答by Anjisan
In ASP.NET you can determine if a JPG is saved using the CMYK profile with the help of the System.Drawing.Imaging.ImageFlags enumeration,
在 ASP.NET 中,您可以借助 System.Drawing.Imaging.ImageFlags 枚举确定是否使用 CMYK 配置文件保存 JPG,
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageflags(VS.80).aspx
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageflags(VS.80).aspx
Let's say you have a basic aspx page where a user uploads a file and you need to tell,
假设您有一个基本的 aspx 页面,用户在其中上传文件,您需要告诉,
1) is it a jpg? 2) is it using RGB?
1) 是 jpg 吗?2)是否使用RGB?
Your aspx might be along the lines of,
你的 aspx 可能是这样的,
<form id="form1" runat="server" enctype="multipart/form-data">
<asp:FileUpload ID="myUpload" runat="server" />
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="Click_submitButton" />
<br /><br />
<asp:Literal ID="feedback" runat="server" />
</form>
and then for your code behind (C#) you can do,
然后对于你背后的代码(C#),你可以这样做,
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Click_submitButton(object sender, EventArgs e)
{
if (myUpload.HasFile && isUploadAJpeg(myUpload.PostedFile))
{
System.Drawing.Bitmap uploadedImage = new System.Drawing.Bitmap(myUpload.PostedFile.InputStream);
if (isFileACMYKJpeg(uploadedImage))
{
feedback.Text = "The file is a CYMK jpg";
}
else
{
feedback.Text = "This is a RGB jpg";
//it is a rgb jpg --> do something with it
}
}
else
{
feedback.Text = "You did not upload a jpg";
}
}
protected bool isUploadAJpeg(HttpPostedFile someFile)
{
if (someFile.ContentType == "image/jpg" || someFile.ContentType == "image/jpeg" || someFile.ContentType == "image/pjpeg")
{
return true;
}
return false;
}
protected bool isFileACMYKJpeg(System.Drawing.Image someImage)
{
System.Drawing.Imaging.ImageFlags flagValues = (System.Drawing.Imaging.ImageFlags)Enum.Parse(typeof(System.Drawing.Imaging.ImageFlags), someImage.Flags.ToString());
if (flagValues.ToString().ToLower().IndexOf("ycck") == -1)
{
return false;
}
return true;
}
}
Hope that helps!
希望有帮助!