-3
我對GDI +相當陌生,想知道是否有人可以指導我如何在C#GDI +中創建此形狀。我在下面包含了我的代碼,目前我只是使用一個圖像,但我寧願只畫一個圖像。有人可以指導我如何在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);
}
你試過畫一個圓圈嗎?那裏有很多例子。一旦你有了這些,最上面的部分只是做一些數學問題,並在其上面繪製一個多邊形。如果你想要一個空心的話,它只會變得複雜。 –
我提到這個是因爲我們需要代碼來幫助你。 http://stackoverflow.com/help/mcve –
如果可能,我寧願將它繪製成一個形狀。 – Cyberdrew