You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何读取圆形位图的楔形区域?

Extracting a Wedge Region from a Circular Bitmap

Hey there! Looks like you've already got a solid start on converting bitmaps to circular images—nice work with that CropToCircle method. Now, to extract a wedge-shaped region from that circular bitmap, we can leverage GDI+'s GraphicsPath and clipping capabilities to target exactly the slice you need. Here's a practical, code-driven solution that fits right in with your existing implementation:

Core Approach

A wedge is essentially a "slice" of a circle, defined by two angles (start and end) from the center. We'll:

  1. Define the wedge shape using GraphicsPath.AddPie() (perfect for creating circular slices)
  2. Use this path to set a clipping region on a new bitmap
  3. Draw your circular source image into this clipped region to isolate the wedge
  4. Fill any empty areas with your desired background color

Implementation Code

Add this method alongside your existing CropToCircle function:

public static Image ExtractWedgeFromCircle(Image circularSrcImage, Color backGround, float startAngle, float sweepAngle)
{
    // Create a destination bitmap matching the source dimensions
    Image dstImage = new Bitmap(circularSrcImage.Width, circularSrcImage.Height, circularSrcImage.PixelFormat);
    using (Graphics g = Graphics.FromImage(dstImage))
    {
        // First, fill the entire bitmap with the background color
        using (Brush br = new SolidBrush(backGround))
        {
            g.FillRectangle(br, 0, 0, dstImage.Width, dstImage.Height);
        }

        // Define the wedge path: AddPie takes (x, y, width, height, startAngle, sweepAngle)
        // We use the full dimensions of the image to match your circular bitmap's bounds
        using (GraphicsPath wedgePath = new GraphicsPath())
        {
            wedgePath.AddPie(0, 0, dstImage.Width, dstImage.Height, startAngle, sweepAngle);
            
            // Set the graphics clip to the wedge shape—only pixels inside this path will be drawn
            g.SetClip(wedgePath);
            
            // Draw the circular source image onto the clipped region
            g.DrawImage(circularSrcImage, 0, 0);
            
            // Reset the clip to clean up
            g.ResetClip();
        }
    }
    return dstImage;
}

Key Details to Note

  • Angle Parameters:
    • startAngle: Measured in degrees, starting from the positive X-axis (right) and moving clockwise. For example, 0 points right, 90 points down, 180 points left, 270 points up.
    • sweepAngle: The number of degrees the wedge spans clockwise from the start angle. A value of 90 gives a quarter-circle wedge, 180 gives a half-circle, etc.
  • Matching Your Circular Bitmap: Since your CropToCircle method creates a circle that fills the entire bitmap bounds, using AddPie(0, 0, width, height, ...) perfectly aligns the wedge with your circular image.
  • Resource Management: We use using statements for all disposable objects (Graphics, Brush, GraphicsPath) to avoid memory leaks—just like your existing code does.

Usage Example

Once you have your circular image from CropToCircle, you can extract a wedge like this:

// Get your circular bitmap first
Image circularImage = CropToCircle(originalBitmap, Color.Transparent);

// Extract a wedge from 0° (right) to 90° (down) — a quarter-circle slice
Image wedgeImage = ExtractWedgeFromCircle(circularImage, Color.Transparent, 0, 90);

Optional: Direct Pixel Manipulation (For Higher Performance)

If you need to work directly with pixel data instead of drawing (e.g., for real-time processing), you can use Bitmap.LockBits to access the pixel buffer. For each pixel, calculate if it falls within the wedge's bounds using trigonometry:

  1. Find the center of the image (centerX = width/2, centerY = height/2)
  2. For each pixel (x,y), calculate its angle relative to the center
  3. Check if the angle falls between startAngle and startAngle + sweepAngle (adjusting for wrap-around past 360°)
  4. Copy the pixel to the destination only if it's inside the wedge

This method is faster for large images but requires more math—stick with the GraphicsPath approach if simplicity is your priority.

内容的提问来源于stack exchange,提问作者Zain Ul Abidin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:36:36