Iphone之所以那么流行一部分归功于他的炫丽的界面,其实那些界面的实现主要由两大功能组成:画透明图片和画渐变效果。这次主要讲述windows mobile下画透明图片。
所谓透明图片其实就是把图片背景设置成透明,如果直接画出图片会显示一个规则正方形的图,其中图片包括不规则图形和该图像下的背景,所以如果只是想画出不规则图形,需要把背景去掉。下面为去掉背景的实现。
////// Draws the image with transparency /// /// Destination graphics /// The image to draw /// Desctination rectangle public static void DrawImageTransparent(this Graphics gx, Image image, Rectangle destRect) { ImageAttributes imageAttr = new ImageAttributes(); Color transpColor = GetTransparentColor(image); imageAttr.SetColorKey(transpColor, transpColor); gx.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); imageAttr.Dispose(); }
private static Color GetTransparentColor(Image image) { return ((Bitmap)image).GetPixel(image.Width - 1, image.Height - 1); }
先调用Bitmap.GetPixel()函数取出图片的背景信息,然后设置ImageAttributes 的属性,在画图的时候把图片属性传递到Graphics.DrawImage()函数进行绘画。
效果图如下:
有些图片做的不好,有点锯齿。
下面是没有去掉背景的图片,作为比较。