.net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
在.net下,如果你加载了一副8位的灰度图像,然后想向其中绘制一些线条、或者填充一些矩形、椭圆等,都需要通过Grahpics.FromImage创建Grahphics对象,而此时会出现:无法从带有索引像素格式的图像创建graphics对象 这个错误,让我们的后续工作无法完成。本文叙述了一种另外的方法来实现它。
我们通过Reflector发编译.net framework的相关函数后发现,FromImage的实现过程如下:
public static Graphics FromImage(Image image)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
if ((image.PixelFormat & PixelFormat.Indexed) != PixelFormat.Undefined)
{
throw new Exception(SR.GetString("GdiplusCannotCreateGraphicsFromIndexedPixelFormat"));
}
IntPtr zero = IntPtr.Zero;
int status = SafeNativeMethods.Gdip.GdipGetImageGraphicsContext(new HandleRef(image, image.nativeImage), out zero);
if (status != )
{
throw SafeNativeMethods.Gdip.StatusException(status);
}
return new Graphics(zero) { backingImage = image };
}
而在MSDN中,对GdipGetImageGraphicsContext函数的描述有如下部分:
This constructor also fails if the image uses one of the following pixel formats:
- PixelFormatUndefined
- PixelFormatDontCare
- PixelFormat1bppIndexed
- PixelFormat4bppIndexed
- PixelFormat8bppIndexed
- PixelFormat16bppGrayScale
- PixelFormat16bppARGB1555
因此,.net是判断当图像为索引模式时,直接返回错误,而不是通过判断GdipGetImageGraphicsContext的返回值来实现的。
针对这个事实,我们其实觉得也无可厚非,Graphics对象是用来干什么的,是用来向对应的Image中添加线条,路径、实体图形、图像数据等的,而普通的索引图像,其矩阵的内容并不是实际的颜色值,而只是个索引,真正的颜色值在调色板中,因此,一些绘制的过程用在索引图像上存在着众多的不适。
但是有个特列,那就是灰度图像,严格的说,灰度图像完全符合索引图像的格式,可以认为是索引图像的一种特例。但是我也可以认为他不属于索引图像一类:即他的图像数据总的值可以认为就是其颜色值,我们可以抛开其调色板中的数据。所以在photoshop中把索引模式和灰度模式作为两个模式来对待。
真是有这个特殊性,一些画线、填充路径等等的过程应该可以在灰度图像中予以实现,单GDI+为了规避过多的判断,未对该模式进行特殊处理。
但是,在一些特殊的场合,对灰度进行上述操作很有用途和意义。比如:在高级的图像设计中,有着选区的概念,而选区的实质上就是一副灰度图像,如果我们创建一个椭圆选区,设计上就是在灰度图像上填充了一个椭圆。如果能借助GDI+提供的优质的抗锯齿填充模式加上丰富自由的填充函数,那么就可以创建出多种多样的选区了。可.net的一个无法创建Graphics让我们此路不通。
有没有办法呢,其实也是有的,熟悉GDI+平板化API的人还知道有GdipCreateFromHDC函数,该函数可以从HDC中创建Graphics。因此我的想法就是利用GDI的方式创建位图对象吗,然后从GDI的HDC中创建对应的Graphics。经过实践,这种方法是可以行的。
为此,我用GDI结合GDI+的方式创建了一个GrayBitmap类,该类的主要代码如下:
unsafe class GrayBitmap
{ #region GDIAPI private const int DIB_RGB_COLORS = ;
private const int BI_RGB = ; [StructLayout(LayoutKind.Sequential, Pack = )]
private struct RGBQUAD
{
internal byte Blue;
internal byte Green;
internal byte Red;
internal byte Reserved;
} [StructLayout(LayoutKind.Sequential, Pack = )]
private struct BITMAPINFOHEADER
{
internal uint Size;
internal int Width;
internal int Height;
internal ushort Planes;
internal ushort BitCount;
internal uint Compression;
internal uint SizeImage;
internal int XPelsPerMeter;
internal int YPelsPerMeter;
internal uint ClrUsed;
internal uint ClrImportant;
}
[StructLayout(LayoutKind.Sequential, Pack = )]
private struct BITMAPINFO
{
internal BITMAPINFOHEADER Header;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
internal RGBQUAD[] Palette;
} [StructLayout(LayoutKind.Sequential)]
internal struct LOGPALETTE
{
internal ushort PalVersion;
internal ushort PalNumEntries;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x400)]
internal byte[] PalPalEntry;
} [DllImport("User32.dll", SetLastError = true)]
private extern static IntPtr GetDC(IntPtr Hwnd); [DllImport("User32.dll", SetLastError = true)]
private extern static int ReleaseDC(IntPtr Hwnd, IntPtr Hdc); [DllImport("Gdi32.dll", SetLastError = true)]
private extern static IntPtr CreateCompatibleDC(IntPtr Hdc); [DllImport("Gdi32.dll", SetLastError = true)]
private static extern uint SetDIBColorTable(IntPtr Hdc, int un1, int un2, RGBQUAD[] pcRGBQUAD); [DllImport("Gdi32.dll", SetLastError = true)]
private static extern IntPtr CreateDIBSection(IntPtr Hdc, ref BITMAPINFO BmpInfo, uint iUsage, out byte* ppvBits, IntPtr hSection, uint dwOffset); [DllImport("Gdi32.dll", SetLastError = true)]
private extern static Boolean DeleteDC(IntPtr Hdc); [DllImport("Gdi32.dll", SetLastError = true)]
private extern static IntPtr SelectObject(IntPtr Hdc, IntPtr Object); [DllImport("Gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr Object); #endregion #region PrivateVariable private int m_Width = ;
private int m_Height = ;
private int m_Stride = ;
private IntPtr m_Hdc = IntPtr.Zero;
private Graphics m_Graphics = null;
private IntPtr m_Handle = IntPtr.Zero;
private byte* m_Pointer = null;
private Bitmap m_Bitmap = null;
private bool Disposed = false; #endregion #region Property public int Width { get { return m_Width; } }
public int Height { get { return m_Height; } }
public int Stride { get { return m_Stride; } }
public IntPtr Handle { get { return m_Handle; } }
public IntPtr Hdc { get { return m_Hdc; } }
public Graphics Graphics { get { return m_Graphics; } }
public byte* Pointer { get { return m_Pointer; } }
public Bitmap Bitmap { get { return m_Bitmap; } } #endregion #region Constructor public GrayBitmap(int Width, int Height)
{
AllocateBitmap(Width, Height);
} public GrayBitmap(string FileName)
{
Bitmap Bmp = (Bitmap)Bitmap.FromFile(FileName);
if (IsGrayBitmap(Bmp) == false)
{
Bmp.Dispose();
throw new Exception("Wrong PixelFormat");
}
else
{
AllocateBitmap(Bmp.Width, Bmp.Height);
BitmapData BmpData = new BitmapData();
BmpData.Scan0 = (IntPtr)m_Pointer;
BmpData.Stride = m_Stride; // 把Image对象的数据拷贝到DIBSECITON中去
Bmp.LockBits(new Rectangle(, , Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite | ImageLockMode.UserInputBuffer, Bmp.PixelFormat, BmpData);
Bmp.UnlockBits(BmpData);
Bmp.Dispose();
}
} public GrayBitmap(Bitmap Bmp)
{
if (IsGrayBitmap(Bmp) == false)
throw new Exception("Wrong PixelFormat");
else
{
AllocateBitmap(Bmp.Width, Bmp.Height);
BitmapData BmpData = new BitmapData();
BmpData.Scan0 = (IntPtr)m_Pointer;
BmpData.Stride = m_Stride; // 把Image对象的数据拷贝到DIBSECITON中去
Bmp.LockBits(new Rectangle(, , Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite | ImageLockMode.UserInputBuffer, Bmp.PixelFormat, BmpData);
Bmp.UnlockBits(BmpData);
}
} ~GrayBitmap()
{
Dispose();
} #endregion #region PublicMethod public static GrayBitmap FromFile(string FileName)
{
GrayBitmap Bmp = new GrayBitmap(FileName);
return Bmp;
} public void Dispose()
{
Dispose(true);
} protected void Dispose(bool Suppress = true)
{
if (Disposed == false)
{
Disposed = true;
if (m_Hdc != IntPtr.Zero) DeleteDC(m_Hdc); m_Hdc = IntPtr.Zero;
if (m_Graphics != null) m_Graphics.Dispose(); m_Graphics = null;
if (m_Bitmap != null) m_Bitmap.Dispose(); m_Bitmap = null;
if (m_Handle != IntPtr.Zero) DeleteObject(m_Handle); m_Handle = IntPtr.Zero;
m_Width = ; m_Height = ; m_Stride = ; m_Pointer = null;
if (Suppress == true) GC.SuppressFinalize(this);
}
} #endregion #region PrivateMethod private void AllocateBitmap(int Width, int Height)
{
if (Width <= ) throw new ArgumentOutOfRangeException("Width", Width, "Width must be >=0");
if (Height <= ) throw new ArgumentOutOfRangeException("Height", Height, "Height must be >=0"); BITMAPINFO BmpInfo = new BITMAPINFO();
BmpInfo.Header.Size = (uint)sizeof(BITMAPINFOHEADER);
BmpInfo.Header.Width = Width;
BmpInfo.Header.Height = -Height; // 为了和GDI对象的坐标系统(起点坐标在左上角),建立一个倒序的DIB
BmpInfo.Header.BitCount = (ushort); ;
BmpInfo.Header.Planes = ;
BmpInfo.Header.Compression = BI_RGB; // 创建DIBSection必须用不压缩的格式
BmpInfo.Header.XPelsPerMeter = ; // CreateDIBSection does not use the BITMAPINFOHEADER parameters biXPelsPerMeter or biYPelsPerMeter and will not provide resolution information in the BITMAPINFO structure.
BmpInfo.Header.YPelsPerMeter = ;
BmpInfo.Header.ClrUsed = ;
BmpInfo.Header.SizeImage = ;
BmpInfo.Header.ClrImportant = ;
BmpInfo.Header.SizeImage = ;
BmpInfo.Palette = new RGBQUAD[];
for (int X = ; X < ; X++) // for (byte X=0;X<=255;X++) 用这个代码试试,呵呵
{
BmpInfo.Palette[X].Red = (byte)X;
BmpInfo.Palette[X].Green = (byte)X;
BmpInfo.Palette[X].Blue = (byte)X;
BmpInfo.Palette[X].Reserved = ;
}
IntPtr ScreecDC = GetDC(IntPtr.Zero);
m_Hdc = CreateCompatibleDC(ScreecDC);
ReleaseDC(IntPtr.Zero, ScreecDC);
m_Handle = CreateDIBSection(Hdc, ref BmpInfo, DIB_RGB_COLORS, out m_Pointer, IntPtr.Zero, );
if (m_Handle == IntPtr.Zero)
{
DeleteDC(m_Hdc);
m_Hdc = IntPtr.Zero;
throw new OutOfMemoryException("CreateDIBSection function failed,this may be caused by user input too large size of image.");
}
else
{
SelectObject(m_Hdc, m_Handle);
SetDIBColorTable(m_Hdc, , , BmpInfo.Palette);
m_Width = Width; m_Height = Height; m_Stride = (int)((m_Width + ) & 0XFFFFFFFC);
m_Graphics = Graphics.FromHdc(m_Hdc);
m_Bitmap = new Bitmap(m_Width, m_Height, m_Stride, PixelFormat.Format8bppIndexed, (IntPtr)m_Pointer);
ColorPalette Pal = m_Bitmap.Palette;
for (int X = ; X < ; X++) Pal.Entries[X] = Color.FromArgb(, X, X, X); // 设置灰度图像的调色板
m_Bitmap.Palette = Pal;
}
} private bool IsGrayBitmap(Bitmap Bmp)
{
bool IsGray;
if (Bmp.PixelFormat == PixelFormat.Format8bppIndexed)
{
IsGray = true;
if (Bmp.Palette.Entries.Length != )
IsGray = false;
else
{
for (int X = ; X < Bmp.Palette.Entries.Length; X++)
{
if (Bmp.Palette.Entries[X].R != Bmp.Palette.Entries[X].G || Bmp.Palette.Entries[X].R != Bmp.Palette.Entries[X].B || Bmp.Palette.Entries[X].B != Bmp.Palette.Entries[X].G)
{
IsGray = false;
break;
}
}
}
}
else
{
IsGray = false;
}
return IsGray;
}
#endregion }
正如上面所述,我们用GDI的方式(CreateDIBSection)创建灰度图像,然后从HDC中创建Graphics,从而可以顺利的调用Graphics的任何绘制函数了。
比如填充椭圆:
SolidBrush SB = new SolidBrush(Color.FromArgb(, , , ));
Bmp.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Bmp.Graphics.FillEllipse(SB, new Rectangle(, , , ));
SB.Dispose();
Canvas.Invalidate();
心细的朋友可以在测试中会发现,通过这种方式绘制的颜色可能和指定的颜色有所不同,比如上面我们要求绘制白色的椭圆,但是实际绘制的颜色是RGB(252,252,252)的,但是并不是所有的颜色都有误差,引起这个的原因估计还是GDI+的内部的一些机制上的问题吧。
工程完整代码:http://files.cnblogs.com/Imageshop/GrayModeBitmap.rar
希望朋友们喜欢我的文章。
***************************作者: laviewpbt 时间: 2013.7.13 联系QQ: 33184777 转载请保留本行信息*************************
.net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。的更多相关文章
- 无法从带有索引像素格式的图像创建graphics对象(转)
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...
- 无法从带有索引像素格式的图像创建graphics对象
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...
- .Net给图片加水印,并解决“无法从带有索引像素格式的图像创建Graphics对象”问题
using (Image img = Image.FromFile(savePath)) { //如果原图片是索引像素格式之列的,则需要转换 if (img.PixelFormat!=null) { ...
- 对索引像素格式的图片进行Setpixel(具有索引像素格式的图像不支持SetPixel)解决方案
最近编写了一个验证码识别软件.其中对png.jpg图片进行二值化处理时,出现了错误:具有索引像素格式的图像不支持SetPixel解决方案.从字面上来看,这说明我对一个具有索引色的图片进行了直接RGB颜 ...
- IntelliJ IDEA创建文件时自动填入作者时间 定制格式
IntelliJ IDEA创建文件时自动填入作者时间 定制格式 学习了:https://blog.csdn.net/Hi_Boy_/article/details/78205483 学习了:http: ...
- .net下灰度模式图像
.net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案. Posted on 2013-07-13 14:23 Imageshop 阅 ...
- 改变MyEclipse创建JSP时默认的pageEncoding编码
如何改变MyEclipse创建JSP时默认的pageEncoding编码 有时我们需要改变MyEclipse创建JSP时默认的pageEncoding编码,因为也许它默认的编码不是我们想要的,比如我们 ...
- Python图像处理丨基于OpenCV和像素处理的图像灰度化处理
摘要:本篇文章讲解图像灰度化处理的知识,结合OpenCV调用cv2.cvtColor()函数实现图像灰度操作,使用像素处理方法对图像进行灰度化处理. 本文分享自华为云社区<[Python图像处理 ...
- Ubuntu 16.04下使用Eclipse:创建工程时卡死的解决方法
问题如下: Ubuntu 16.04下使用Eclipse创建工程时出现卡顿和卡死,新建一个MapReduce项目卡了一下午,鼠标变成了圆圈进度条转了一下午,还关不掉. 当我直接去关闭新建项目的窗口时, ...
随机推荐
- .NET基础架构方法—DataTableToExcel通用方法
p { display: block; margin: 3px 0 0 0; } --> .NET架构基础方法—DataTableToExcel通用方法(NPOI) 今天封装DataTaleTo ...
- Git避免输入用户名和密码
1. 在系统盘/用户/用户名目录下新建名为 _netrc 的文件: 2. 修改 _netrc 文件内容为“ machine git.mangning.comlogin xxxpassword xxx ...
- 疯狂Android讲义 - 学习笔记(六)
第7章 7.1 使用简单图片 7.1.1 使用Drawable对象 7.1.2 Bitmap和BitmapFactory 7.2 绘图 7.2.1 Android绘图基础:Canvas.Paint等 ...
- Linux-网络连接-(VMware与CentOS)
VMware虚拟机中安装CentOS,进行网络连接,分为两步,内网连接,与外网连接. 前提: 当你正确安装VMware后,网络适配器会增加2个新的网卡:(可在设备管理器->网络适配器中查看) 第 ...
- 定制自己的mybatis生成
MyBatis Generator原生提供的生成方式targetRuntime有几种,但都不符合项目需求或想自定义自己的方法. 网上的文章也很多: 如:http://generator.sturgeo ...
- Could not publish server configuration for Tomcat v6.0 Server at localhost.
经常在使用tomcat服务器的时候 总会发生一些莫名其妙的错误. 就像下面这个错误: 在配置文件中存在多个/MyWeb的配置,导致不能发布服务. 错误信息: Could not publish ser ...
- 原生JS实战:分享一个首页进度加载动画!
本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5871134.html 该程序是本人的个人作品,写的不好,可以参考,但未经 ...
- javascript 函数初探 (四)--- 回调函数
回调函数 既然函数与任何被赋值给变量的数据是相同的,那么她当然可以像其他数据那样被定义.删除.拷贝,以及当成参数传递给其它函数. 我们定义一个函数,这个函数有两个函数类型的参数,然后他会分别执行这两个 ...
- css相对定位和绝对定位
相对定位,是对原来元素的位置为参照物进行定位: 绝对定位,如果父级没有定位,则针对HTML为参照物进行定位:如果父级有定位,则针对父元素为参照物进行定位
- IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法
当打开IIS管理器,或配置网站时提示错误:配置文件的XML格式不正确 且是applicationHost.config的问题,那么肯定是applicationHost.config被破坏,IIS就崩溃 ...