| 
 | 
 
 
 楼主 |
发表于 2023-3-25 15:48:41
|
显示全部楼层
 
 
 
public async Task<BitmapSource> RenderCirclesAsync(int count, double radius, double width, double height) 
{ 
    // create a new RenderTargetBitmap with the specified width and height 
    RenderTargetBitmap renderTarget = new RenderTargetBitmap( 
        (int)width, (int)height, 96, 96, PixelFormats.Pbgra32); 
 
    // create a new DrawingVisual for rendering the circles 
    DrawingVisual drawingVisual = new DrawingVisual(); 
 
    // create a new Random instance for generating random circle positions 
    Random random = new Random(); 
 
    // create a new SolidColorBrush for filling the circles 
    SolidColorBrush fillBrush = new SolidColorBrush(Colors.Blue); 
 
    // create a new Pen for outlining the circles 
    Pen strokePen = new Pen(Brushes.Black, 1.0); 
 
    // render the circles asynchronously 
    await Task.Run(() => 
    { 
        using (DrawingContext drawingContext = drawingVisual.RenderOpen()) 
        { 
            for (int i = 0; i < count; i++) 
            { 
                double x = random.NextDouble() * width; 
                double y = random.NextDouble() * height; 
 
                drawingContext.DrawEllipse(fillBrush, strokePen, new Point(x, y), radius, radius); 
            } 
        } 
    }); 
 
    // render the drawing visual to the RenderTargetBitmap 
    renderTarget.Render(drawingVisual); 
 
    // return the rendered bitmap source 
    return renderTarget; 
} 
 |   
 
 
 
 |