|
楼主 |
发表于 2023-3-21 17:35:55
|
显示全部楼层
For the first suggestion, using a DrawingVisual, you can create a custom class like this:
public class CircleVisual : DrawingVisual
{
public CircleVisual(double x, double y, double radius)
{
using (DrawingContext dc = RenderOpen())
{
dc.DrawEllipse(Brushes.White, new Pen(Brushes.White, 0.1), new Point(x,y), radius, radius);
}
}
}
Then you can add instances of this class to a VisualHost element in your XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<VisualHost x:Name="visualHost"/>
</Grid>
</Window>
And in your code-behind, you can loop through your data and create circles like this:
foreach (var item in DieControl.ThrdResultData.BumpHeighList.BumpHeight)
{
CircleVisual circle = new CircleVisual(item.BumpPosX * scaleX, item.BumpPosY * scaleY, 25 * scaleX);
visualHost.Children.Add(circle);
}
For the second suggestion, using a WriteableBitmap, you can create a bitmap and add it to an Image element in your XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image x:Name="image"/>
</Grid>
</Window>
And in your code-behind, you can create a bitmap and draw circles on it using the WritePixels method:
WriteableBitmap bitmap = new WriteableBitmap((int)DieControl.MapCanvas.ActualWidth,
(int)DieControl.MapCanvas.ActualHeight,
96,
96,
PixelFormats.Bgra32,
null);
for (int i = 0; i < DieControl.ThrdResultData.BumpHeighList.BumpHeight.Count; i++)
{
int centerX = (int)(DieControl.ThrdResultData.BumpHeighList.BumpPosX[i] * scaleX);
int centerY = (int)(DieControl.ThrdResultData.BumpHeighList.BumpPosY[i] * scaleY);
int radius = (int)(25 * scaleX);
// Create an array of color values for the circle pixels
byte[] pixels = new byte[radius * radius * 4];
// Fill the array with white color values
for (int j = 0; j < pixels.Length; j += 4)
{
pixels[j] = 255; // blue
pixels[j + 1] = 255; // green
pixels[j + 2] = 255; // red
pixels[j + 3] = 255; // alpha
}
// Write the pixels to the bitmap at the circle center position
bitmap.WritePixels(new Int32Rect(centerX - radius / 2,
centerY - radius / 2,
radius,
radius),
pixels,
radius * 4,
0);
}
// Set the image source to the bitmap
image.Source = bitmap; |
|