【问题标题】:How do I display an image within a region defined in a Picturebox?如何在 Picturebox 中定义的区域内显示图像?
【发布时间】:2009-08-24 19:25:26
【问题描述】:

作为我上一个问题的后续,我已经获得了我的区域,但在过去的两个小时里试图仅显示该区域的小图片;最终目标是能够任意显示我选择的任意数量的图像。 到目前为止,这是我的代码:

    void OnPaintRadar(Object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;        
    Bitmap blip = new Bitmap(tst_Graphics.Properties.Resources.dogtag);
    Rectangle radar_rect = new Rectangle(myRadarBox.Left + 80, myRadarBox.Left + 7, myRadarBox.Width - 200, myRadarBox.Height + 200);
    using (Pen drw_pen = new Pen(Color.White, 1) )
    {
        using (GraphicsPath path = new GraphicsPath())
        {
            path.AddPie(radar_rect, 180, 180);
            path.CloseFigure();
            using (Region rdRegion = new Region(path) )
            {
                g.DrawPath(drw_pen, path);
                g.Clip = rdRegion;
                PictureBox pb = new PictureBox();
                pb.Image = (blip);
                pb.Size = blip.Size;
                g.DrawImage(blip, radar_rect);
            }
        }

    }

}// end paint method

我也尝试过 DrawImageUnscaled 方法,但我要么把我的小图片炸掉以填充饼图区域,要么什么都不显示。

【问题讨论】:

    标签: c# .net graphics


    【解决方案1】:

    Click here 运行一个示例应用程序,演示如何做雷达的基础知识(或至少一种方式)。注意:此应用程序不对小图像进行双缓冲或透明处理。

    该项目的源代码是here

    【讨论】:

    • 非常感谢。我会看一下,稍后再回复你。
    【解决方案2】:

    这一行:

    pb.Image = (blip);
    

    是导致小图像看起来很大的原因。基本上,您从资源中提取一个小位图,然后将 PictureBox 的 Image 属性设置为该位图(我假设“pb”是您表单上的一个 PictureBox)。尝试注释掉该行及其下方的行。

    【讨论】:

    • 不高兴。实际上我只是想,因为我没有使用图片框,所以注释掉的行不会影响任何事情,因为我将图像而不是图片框添加到该区域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2013-08-13
    • 2015-06-29
    相关资源
    最近更新 更多