using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing.Drawing2D; using System.Drawing; using System.Runtime.InteropServices; namespace Lskj.Control.Commom { /// /// 控件工具类 /// public class ControlCommom { /// /// 转换成圆角 /// /// 要转换的rectf /// 圆角半径的大小 /// public static GraphicsPath TransformCircular(RectangleF rectf, float radius = 0) { return TransformCircular(rectf, radius, radius, radius, radius); } /// /// 转换成圆角 /// /// 要转换的rectf /// 左上角 /// 右上角 /// 右下角 /// 左下角 /// public static GraphicsPath TransformCircular(RectangleF rectf, float leftTopRadius = 0f, float rightTopRadius = 0f, float rightBottomRadius = 0f, float leftBottomRadius = 0f) { GraphicsPath gp = new GraphicsPath(); if (leftTopRadius > 0) { RectangleF lefttop_rect = new RectangleF(rectf.X, rectf.Y, leftTopRadius * 2, leftTopRadius * 2); gp.AddArc(lefttop_rect, 180, 90); } else { gp.AddLine(new PointF(rectf.X, rectf.Y), new PointF(rightTopRadius > 0 ? rectf.Right - rightTopRadius * 2 : rectf.Right, rectf.Y)); } if (rightTopRadius > 0) { RectangleF righttop_rect = new RectangleF(rectf.Right - rightTopRadius * 2, rectf.Y, rightTopRadius * 2, rightTopRadius * 2); gp.AddArc(righttop_rect, 270, 90); } else { gp.AddLine(new PointF(rectf.Right, rectf.Y), new PointF(rectf.Right, rightBottomRadius > 0 ? rectf.Bottom - rightTopRadius * 2 : rectf.Bottom)); } if (rightBottomRadius > 0) { RectangleF rightbottom_rect = new RectangleF(rectf.Right - rightTopRadius * 2, rectf.Bottom - rightTopRadius * 2, rightBottomRadius * 2, rightBottomRadius * 2); gp.AddArc(rightbottom_rect, 0, 90); } else { gp.AddLine(new PointF(rectf.Right, rectf.Bottom), new PointF(leftBottomRadius > 0 ? leftBottomRadius * 2 : rectf.X, rectf.Bottom)); } if (leftBottomRadius > 0) { RectangleF rightbottom_rect = new RectangleF(rectf.X, rectf.Bottom - leftBottomRadius * 2, leftBottomRadius * 2, leftBottomRadius * 2); gp.AddArc(rightbottom_rect, 90, 90); } else { gp.AddLine(new PointF(rectf.X, rectf.Bottom), new PointF(rectf.X, leftTopRadius > 0 ? rectf.X + leftTopRadius * 2 : rectf.X)); } gp.CloseAllFigures(); return gp; } /// /// 根据画笔大小计算出真是rectf /// /// 要转换的rectf /// 画笔大小大小 /// public static RectangleF TransformRectangleF(RectangleF rectf, float pen) { RectangleF result = new RectangleF(); result.Width = rectf.Width - (pen < 1 ? 0 : pen); result.Height = rectf.Height - (pen < 1 ? 0 : pen); result.X = rectf.X + (pen / 2f); result.Y = rectf.Y + (pen / 2f); return result; } /// /// 倒影变换 /// /// 原图片 /// 倒影边距 /// 明亮度 /// 倒影开始透明度 /// 倒影结束透明度 /// 倒影高度 /// public static Bitmap TransformReflection(Bitmap bmp, int reflectionTop = 10, int reflectionBrightness = -50, int reflectionTransparentStart = 200, int reflectionTransparentEnd = -0, int reflectionHeight = 50) { /// /// 图片最终高度 /// int finallyHeight = bmp.Height + reflectionTop + reflectionHeight; Color pixel; int transparentGradient = 0;//透明梯度 transparentGradient = (reflectionTransparentEnd - reflectionTransparentStart) / reflectionHeight; if (transparentGradient == 0) transparentGradient = 1; Bitmap result = new Bitmap(bmp.Width, finallyHeight); Graphics graphic = Graphics.FromImage(result); graphic.DrawImage(bmp, new RectangleF(0, 0, bmp.Width, bmp.Height)); graphic.Dispose(); for (int y = 0; y < reflectionHeight; y++) { for (int x = 0; x < bmp.Width; x++) { pixel = bmp.GetPixel(x, bmp.Height - 1 - y); int a = VerifyRGB(reflectionTransparentStart + y * transparentGradient); if (pixel.A == 0 || pixel.A < a) { result.SetPixel(x, bmp.Height - 1 + reflectionTop + y, pixel); } else { int r = VerifyRGB(pixel.R + reflectionBrightness); int g = VerifyRGB(pixel.G + reflectionBrightness); int b = VerifyRGB(pixel.B + reflectionBrightness); result.SetPixel(x, bmp.Height - 1 + reflectionTop + y, Color.FromArgb(a, r, g, b)); } } } return result; } /// /// 检查RGB值ed有效范围 /// /// /// public static int VerifyRGB(int rgb) { if (rgb < 0) return 0; if (rgb > 255) return 255; return rgb; } /// /// 计算指定角度的坐标 /// /// 圆心坐标 /// 圆半径 /// 角度 /// public static PointF CalculatePointForAngle(PointF center, float radius, float angle) { if (radius == 0) return center; float w = 0; float h = 0; if (angle <= 90) { w = radius * (float)Math.Cos(Math.PI / 180 * angle); h = radius * (float)Math.Sin(Math.PI / 180 * angle); } else if (angle <= 180) { w = -radius * (float)Math.Sin(Math.PI / 180 * (angle - 90)); h = radius * (float)Math.Cos(Math.PI / 180 * (angle - 90)); } else if (angle <= 270) { w = -radius * (float)Math.Cos(Math.PI / 180 * (angle - 180)); h = -radius * (float)Math.Sin(Math.PI / 180 * (angle - 180)); } else { w = radius * (float)Math.Sin(Math.PI / 180 * (angle - 270)); h = -radius * (float)Math.Cos(Math.PI / 180 * (angle - 270)); } return new PointF(center.X + w, center.Y + h); } /// /// 根据画笔大小转换rectf /// /// 要转换的rectf /// 画笔大小大小 /// public static RectangleF TransformRectangleByPen(RectangleF rectf, float pen) { RectangleF result = new RectangleF(); result.Width = rectf.Width - (pen < 1 ? 0 : pen); result.Height = rectf.Height - (pen < 1 ? 0 : pen); result.X = rectf.X + (float)(pen / 2); result.Y = rectf.Y + (float)(pen / 2); return result; } /// /// 结构转指针 /// /// 结构类型 /// /// public static IntPtr StructToIntPtr(T info) { int size = Marshal.SizeOf(info); IntPtr intPtr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(info, intPtr, true); return intPtr; } /// /// 指针转结构 /// /// 结构类型 /// /// public static T IntPtrToStruct(IntPtr info) { return (T)Marshal.PtrToStructure(info, typeof(T)); } } }