C#使用GDI中的API函数
我们知道在.NET 平台下主要是用GDI+来进行图形图像处理,在效率要求不高的情况下使用GDI+已经足够实现各种功能了,但一旦要求效率的情况下,我们可以考虑使用GDI来代替GDI+,网上有人士做过相关测试(本人也测试过),GDI在图形、图像绘制方面效率较GDI+有很大提高。下面将自己开发过程中整理到的NativeGdi32Api类贴出来:
publicstaticclassNativeGdi32Api{[DllImport("gdi32.dll")]publicstaticexternintSetDIBits(IntPtrhdc,IntPtrhBitmap,intnStartScan,intnNumScans,IntPtrlpBits,IntPtrlpBI,intwUsage);[DllImport("gdi32.dll")]publicstaticexternintSetDIBitsToDevice(IntPtrhdc,intx,inty,intdx,intdy,intSrcX,intSrcY,intScan,intNumScans,IntPtrBits,IntPtrBitsInfo,intwUsage);[DllImport("gdi32.dll")]publicstaticexternIntPtrCreateDIBSection(IntPtrhdc,IntPtrpBitmapInfo,intun,IntPtrlplpVoid,IntPtrhandle,intdw);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternintSetPixel(IntPtrhdc,intx,inty,intcrColor);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetPixel(IntPtrhdc,intx,inty);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternintCombineRgn(IntPtrdest,IntPtrsrc1,IntPtrsrc2,intflags);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternIntPtrCreateBrushIndirect(refLOGBRUSHbrush);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternIntPtrCreateRectRgnIndirect(refRECTAPIrect);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetClipBox(IntPtrhDC,refRECTAPIrectBox);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternboolPatBlt(IntPtrhDC,intx,inty,intwidth,intheight,uintflags);[DllImport("gdi32.dll",CharSet=CharSet.Auto)]publicstaticexternintSelectClipRgn(IntPtrhDC,IntPtrhRgn);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrMoveToEx(IntPtrhDC,intx,inty,refPOINTAPIlpPoint);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrLineTo(IntPtrhDC,intx,inty);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreatePen(intnPenStyle,intnWidth,intcrColor);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternintSetBrushOrgEx(IntPtrhDC,intx,inty,refPOINTAPIp);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreatePatternBrush(IntPtrhBMP);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternintGetTextFace(IntPtrhDC,intnCount,stringlpFacename);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternintGetTextMetrics(IntPtrhDC,refGDITextMetricTextMetric);[DllImport("gdi32.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreateFontIndirect([MarshalAs(UnmanagedType.LPStruct)]LogFontLogFont);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintBitBlt(IntPtrhDestDC,intx,inty,intnWidth,intnHeight,IntPtrhSrcDC,intxSrc,intySrc,intdwRop);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreateSolidBrush(intcrColor);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternintRectangle(IntPtrhDC,intleft,inttop,intright,intbottom);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreateHatchBrush(intStyle,intcrColor);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreateCompatibleBitmap(IntPtrhDC,intnWidth,intnHeight);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrCreateCompatibleDC(IntPtrhDC);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrSelectObject(IntPtrhDC,IntPtrhObject);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrDeleteObject(IntPtrhObject);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintGetTextColor(IntPtrhDC);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintSetTextColor(IntPtrhDC,intcrColor);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintGetBkColor(IntPtrhDC);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintGetBkMode(IntPtrhDC);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternIntPtrDeleteDC(IntPtrhDC);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintSetBkColor(IntPtrhDC,intcrColor);[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]publicstaticexternintSetBkMode(IntPtrhDC,intMode);[DllImport("gdi32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]publicstaticexternintGdiFlush();[DllImport("gdi32.dll",SetLastError=true,CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]publicstaticexternintEnumFontFamiliesEx(IntPtrhDC,[MarshalAs(UnmanagedType.LPStruct)]LogFontlf,FONTENUMPROCproc,Int64LParam,Int64DW);[DllImport("gdi32.dll",EntryPoint="GdiAlphaBlend")]publicstaticexternboolAlphaBlend(IntPtrhdcDest,//handletodestinationDCintnXOriginDest,//x-coordofupper-leftcornerintnYOriginDest,//y-coordofupper-leftcornerintnWidthDest,//destinationwidthintnHeightDest,//destinationheightIntPtrhdcSrc,//handletosourceDCintnXOriginSrc,//x-coordofupper-leftcornerintnYOriginSrc,//y-coordofupper-leftcornerintnWidthSrc,//sourcewidthintnHeightSrc,//sourceheightBLENDFUNCTIONblendFunction//alpha-blendingfunction);}
另外,如果真的是对效率要求很高的应用,还是推荐大家使用OpenGL、DirectX。.NET平台下有与之对应的Tao.OpenGL和Managed DirectX。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。