我对GDI +相当新,并且想知道是否有人可以指导我如何在C#GDI +中创建这个形状。我在下面包含了我的代码,目前我只使用了一个图像,但我更愿意只是绘制它。
我用这个作为参考:
public static byte[] RangeGauge(GaugeData data, int size, ImageFormat format)
{
var min = data.Ranges.Min(t => t.MinValue);
var max = data.Ranges.Max(t => t.MaxValue);
var rangeTotal = max - min;
var value = data.Value;
var valueAngle = (((value - min) / rangeTotal) * 270) + 135;
using (var bmp = new Bitmap(500, 500))
{
bmp.SetResolution(300, 300);
using (var g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
var rec = new Rectangle(0, 0, 500, 500);
g.FillPie(new SolidBrush(Color.White), rec, 45, 90);
var startDeg = 135f;
foreach (var item in data.Ranges.OrderBy(o => o.MinValue))
{
g.FillPie(new SolidBrush(item.TrueColor), rec, startDeg, (float)(((item.MaxValue - item.MinValue) / rangeTotal) * 270));
startDeg = (float)(startDeg + (((item.MaxValue - item.MinValue) / rangeTotal) * 270));
}
g.FillEllipse(new SolidBrush(Color.White), new Rectangle(125, 125, 250, 250));
//HERE is where I would rather draw needle
using (var needle = Graphics.FromImage(bmp))
{
needle.TranslateTransform(250, 250);
needle.RotateTransform((float)valueAngle);
needle.TranslateTransform(-105, -105);
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("LoggingService.Needle.png");
needle.DrawImage(Image.FromStream(myStream), new PointF(0, 0));
}
g.FillEllipse(new SolidBrush(Color.White), new Rectangle(175, 175, 150, 150));
var sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(value.ToString(), new Font(new FontFamily("Impact"), 18, FontStyle.Regular), new SolidBrush(Color.Black),
new PointF(250, 250), sf);
g.DrawString(data.Label, new Font(new FontFamily("Impact"), 12, FontStyle.Regular), new SolidBrush(Color.Black),
new PointF(250, 425), sf);
}
using (var ms = new MemoryStream())
{
if (size == 500)
{
bmp.Save(ms, format);
return ms.ToArray();
}
var newBmp = new Bitmap(bmp, size, size);
bmp.SetResolution(300, 300);
newBmp.Save(ms, format);
return ms.ToArray();
}
}
}
public class GaugeData
{
public Range[] Ranges { get; set; }
public double Value { get; set; }
public string Label { get; set; }
}
public class Range
{
public double MinValue { get; set; }
public double MaxValue { get; set; }
public string Color { get; set; }
internal Color TrueColor => (Color) new ColorConverter().ConvertFromString(Color);
}
答案 0 :(得分:1)
GraphicsPath.AddPie + AddLine + AddLine + CloseFigure取决于您的参数。