检查Asp.net中的上传图像颜色 [英] Check Uploaded Image Color in Asp.net

查看:174
本文介绍了检查Asp.net中的上传图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以告诉我如何检查文件上传的图片是彩色还是黑色&白色。

实际上在我的项目中,客户要求我检查用户是否只能上传彩色图像。



谢谢,

Naveen Sharma

Hi,
Can some one please tell me how to check the image uploaded by the file upload is colored or black & white.
Actually in my project the client asked me to check that user can only upload the color images.

Thanks,
Naveen Sharma

推荐答案

它与Web应用程序的任何内容无关。它取决于图像文件格式。有太多东西无法描述它们。



基本上,图像文件的读者读取标题部分并提取一些元数据。它始终包含图像大小和其他数据,最常见的是像素格式。像素格式告诉您每个像素使用了多少数据以及它在代表像素的存储器字中的排列方式。



您可以在<$ c $中找到所有数据c> System.Drawing 和 System.Drawing.Imaging 。请参阅:

http://msdn.microsoft.com /en-us/library/system.drawing.bitmap.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.drawing.image.pixelformat.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat .aspx [ ^ ]。



有什么好处:你可以阅读任何一个文件支持的格式仅使用一个类 System.Drawing.Bitmap



类<$唯一的问题 System.Drawing.Bitmap :要获取像素格式,您需要从流中读取完整图像;当你看到它错了时,你不能只阅读顶部并取消阅读;它没有钩子。所以,准备好阅读整个文件。



-SA
It is not related to anything of the Web application. It rather depends on the image file format. There are too many to describe them all.

Basically, the reader of image files reads the header part and extract some metadata. It always contains the image size and other data, most typically, "pixel format". Pixel format tells you how much data per pixel is used and how it is arranged in the memory word representing a pixel.

You can find it all in System.Drawing and System.Drawing.Imaging. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.image.pixelformat.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx[^].

What''s good about it: you can read any files of one of supported formats using just one class System.Drawing.Bitmap.

The only problem with the class System.Drawing.Bitmap: to get the pixel format, you will need to read the full image from the stream; you cannot read just the top part and cancel reading when you see it''s wrong; there is no a hook into it. So, get ready to read the whole file.

—SA


我找到了一些解决方案给你。



1. 如何检测图像颜色调色板? [ ^ ]。检查解决方案。它提供了一些逻辑和代码,通过获取像素细节来获取图像中使用的颜色。

I found some solutions for you.

1. How to detect image color palette?[^]. Check the solution. It gives some logic and code to get the colors used in the image by getting the pixel details.



Bitmap bmp = (Bitmap)Bitmap.FromFile("image.jpg");
for (int x = 0; x < bmp.Width; x++)
{
    for (int y = 0; y < bmp.Height; y++)
    {
        Color thisColor = bmp.GetPixel(x, y);
        // do your logic for count the color instances... 
    }
}






2.您可以使用 Image.Palette Property [ ^ ],


2. You can use Image.Palette Property[^], which



获取或设置用于此图像的调色板。


Gets or sets the color palette used for this Image.



ColorPalette类 [ ^ ],


and ColorPalette Class[^], which



定义构成调色板的颜色数组。颜色为32位ARGB颜色。不可继承。


Defines an array of colors that make up a color palette. The colors are 32-bit ARGB colors. Not inheritable.



最后 ColorPalette.Entries属性 [ ^ ]

.
Finally ColorPalette.Entries Property[^]



获取Color结构数组。


Gets an array of Color structures.



所以,试着实施。



参考文献

1. Image Class [ ^ ]。

2. 调色板生成器 [ ^ ] - 在线演示。


So, try to implement.

References
1. Image Class[^].
2. Color Palette Generator[^] - Online demo.


这篇关于检查Asp.net中的上传图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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