有没有一种方法可以通过读取图像标题来了解png图像的透明度?
参考方案
二进制访问
在评论每个像素GetPixel
的性能不佳之后,我尝试编写一个代码段,以查找图像(包括PNG)中是否存在透明像素。这里是。
public static bool IsImageTransparent(string fullName)
{
using (Bitmap bitmap = Bitmap.FromFile(fullName) as Bitmap)
{
bool isTransparent;
// Not sure if the following enumeration is correct. Maybe some formats do not actually allow transparency.
PixelFormat[] formatsWithAlpha = new[] { PixelFormat.Indexed, PixelFormat.Gdi, PixelFormat.Alpha, PixelFormat.PAlpha, PixelFormat.Canonical, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed, PixelFormat.Format8bppIndexed, PixelFormat.Format16bppArgb1555, PixelFormat.Format32bppArgb, PixelFormat.Format32bppPArgb, PixelFormat.Format64bppArgb, PixelFormat.Format64bppPArgb };
if (formatsWithAlpha.Contains(bitmap.PixelFormat))
{
// There might be transparency.
BitmapData binaryImage = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format64bppArgb);
unsafe
{
byte* pointerToImageData = (byte*)binaryImage.Scan0;
int numberOfPixels = bitmap.Width * bitmap.Height;
isTransparent = false;
// 8 bytes = 64 bits, since our image is 64bppArgb.
for (int i = 0; i < numberOfPixels * 8; i += 8)
{
// Check the last two bytes (transparency channel). First six bytes are for R, G and B channels. (0, 32) means 100% opacity.
if (pointerToImageData[i + 6] != 0 || pointerToImageData[i + 7] != 32)
{
isTransparent = true;
break;
}
}
}
bitmap.UnlockBits(binaryImage);
}
else
{
// No transparency available for this image.
isTransparent = false;
}
return isTransparent;
}
}
优点:
二进制访问,比GetPixel
快得多,
不需要其他库也不需要WPF,
适用于GDI +支持的任何格式:BMP,GIF,JPEG,PNG,TIFF,Exif,WMF和EMF。
缺点:
需要unsafe
,
比直接读取PNG文件要慢。
调色板
较不手动的方法是使用调色板。可能存在一些.NET Framework或第三方库,您可以通过这样做。我尝试了以下操作(使用WPF):
using (Stream imageStreamSource = new FileStream(fullName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
return bitmapSource.Palette.Colors.Any(c => c.A != 0);
}
但由于大多数情况下bitmapSource.Palette
是null
,所以我无法正常工作。而且,与第一个片段相比,使用调色板会严重降低性能,因为在继续操作之前必须将每种颜色加载到颜色列表中。
我有一个ASP.NET MVC网页,该网页基本上通过日期过滤器显示MS SQL数据库中表的行。当新行插入数据库表时,我想用新行列表更新网页视图。实现此目标的最有效方法是什么?基本上,我想从我的JavaScript创建一个到数据库服务器的回调,以用新结果更新UI。假设数据库表中的行数很大。(〜1百万)谢谢,cas 参考方案 如果数据库更新非常频繁,则可以按特定…
如何使用JavaScript访问嵌入式ASP.NET GlobalResources? - javascript我正在开发一个遗留的ASP.NET项目,该项目正试图缓慢地进行调整,但是如果没有像巧克力手指屋一样塌陷的情况,我将无法进行重大更改。我试图为此找到解决方案,但由于术语的特定混合(“ javascript”,“ embedded”和/或“ resource”只是为我提供了有关如何嵌入.js文件的信息,而失败了)。 。),这可能是一种怪异的处理方式。该项目将Ap…
如何使用ASP.NET ViewState使用JavaScript - c#我的页面中有UL,它为空。我开始使用JavaScript使用LI填充它。在回发阶段如何在asp.net中使用此新添加的动态数据?那是因为我的提交按钮是asp.net控件。我不想使用JS POST。谢谢 参考方案 我过去通过在隐藏字段中填充要发布的动态数据来完成此类操作,您可以-在回传之前触发JavaScript事件,该事件将数据从UL解析到隐藏字段中要么更新…
如何从.Net DLL获取公共出口列表? - c#我可以使用“ dumpbin”和“ dll export”之类的工具来查看标准win32 DLL的公共入口点(“ exports”),例如Windows \ SYSTEM32 \ GDI32.dll。但是,当我在.Net DLL上使用这些相同的工具时,我看到的仅仅是 2000 .reloc 2000 .rsrc 48000 .text 我有一个C#/。Net…
VB.NET C#语法的泛型 - c#您能否告诉我此VB.NET代码的C#等效项:Public Partial Class Index Inherits System.Web.Mvc.Viewpage(Of List(Of Task)) End Class 我不确定在哪里/如何在C#中添加它:public partial class DirList : System.Web.Mvc.ViewPa…