小弟用winfrom做了一个调用摄像头拍照,让一个panel或者picturebox呈现图像,想在这个上面做一个可选区域,但是都失败了,如果用panel做呈像,不管把什么放到panel里面都不到了,如果用picturebox就可以出现一个层,但是完全遮住了,根本透明不了。有没有什么方法可以在上面做一个可选区域。

解决方案 »

  1.   

    可选区域什么的不明白,但是panel透明是可以的
      

  2.   

    用控件透明还算有点麻烦,你不如自己绘制一个,在graphic上划出区域矩形,color支持alpha通道,这个就可以透明
      

  3.   

                float opacity = 0;
                float[][] nArray ={ new float[] {1, 0, 0, 0, 0}, 
                                    new float[] {0, 1, 0, 0, 0}, 
                                    new float[] {0, 0, 1, 0, 0}, 
                                    new float[] {0, 0, 0, opacity, 0}, 
                                    new float[] {0, 0, 0, 0, 1}
                                   };            while (opacity < 1) {
                    opacity += 0.1F;
                    nArray[3][3] = opacity;
                    ColorMatrix matrix = new ColorMatrix(nArray);
                    ImageAttributes attributes = new ImageAttributes();
                    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    Bitmap resultImage = new Bitmap(img.Width, img.Height);
                    Graphics g = Graphics.FromImage(resultImage);
                    g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attributes);
                    pPicBox.Image.Dispose();
                    pPicBox.Image = resultImage;
                    Thread.Sleep(100);
                }
      

  4.   

    直接写一个控件继承自usercontrol
    在onPaint()事件里绘图
    (1)绘图用drawbitmap
    (2)绘矩形什么的用drawrectangle
    再做鼠标事件控制就可以了,不用搞什么透明不透明的
      

  5.   

    绘制大功告成,只不过没有看到懂下面贴出代码:    public partial class EditPhoto : Form
        {        private Rectangle m_Rect;
            private Point m_LastMsPoint;
            private Point m_LastRectPoint;
            private bool m_CanMove;
            int mouseflag = 0;
            Cursor m_oldCursor = default(Cursor);        public EditPhoto()
            {
                InitializeComponent();
                m_Rect = new Rectangle(10, 10, 50, 25);
            }
            string Path = "\\photo\\";
            private void EditPhoto_Load(object sender, EventArgs e)
            {
                pictureBox1.ImageLocation = Application.StartupPath + Path + "1" + ".bmp";
                //pictureBox1.ImageLocation = CanPhotosForm.picurl;
            }        private void btnxuanqu_Click(object sender, EventArgs e)
            {
                Image newImage = pictureBox1.Image;
                int towidth = m_Rect.Width;
                int toheight = m_Rect.Height;
                int x = m_Rect.X;
                int y = m_Rect.Y;
                int ow = m_Rect.Width;
                int oh = m_Rect.Height;
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);            //新建一个画板
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);            //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;            //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            //清空画布并以透明背景色填充
                g.Clear(System.Drawing.Color.Transparent);            //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(newImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);            try
                {
                    //以jpg格式保存缩略图
                    bitmap.Save(Application.StartupPath + Path + textBox1.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                   
                }
                finally
                {
                   //newImage.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }        }              private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawRectangle(SystemPens.HotTrack, this.m_Rect);
            }
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                #region zhege
                Rectangle rect_left = new Rectangle(m_Rect.Left - 2, m_Rect.Top + 2, 4, m_Rect.Height - 4);
                Rectangle rect_right = new Rectangle(m_Rect.Right - 2, m_Rect.Top + 2, 4, m_Rect.Height - 4);
                Rectangle rect_top = new Rectangle(m_Rect.Left + 2, m_Rect.Top - 2, m_Rect.Width - 4, 4);
                Rectangle rect_bottom = new Rectangle(m_Rect.Left + 2, m_Rect.Bottom - 2, m_Rect.Width - 4, 4);
                Rectangle rect_rb = new Rectangle(m_Rect.Right - 2, m_Rect.Bottom - 2, 4, 4);
                if (m_Rect.Contains(e.Location))
                {
                    m_CanMove = false;
                }
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    if (mouseflag != 0)
                    {
                        switch (mouseflag)
                        {
                            case 4:
                                this.m_Rect = new Rectangle(e.X, m_Rect.Top, m_Rect.Right - e.X, m_Rect.Height);
                                break;                        case 6:
                                this.m_Rect.Width = e.X - m_Rect.Left;
                                break;                        case 8:
                                this.m_Rect.Height = m_Rect.Bottom - e.Y;
                                m_Rect.Location = new Point(m_Rect.Left, e.Y);
                                break;                        case 2:
                                this.m_Rect.Height = e.Y - m_Rect.Top;
                                break;                        case -1:
                                this.m_Rect.Width = e.X - m_Rect.Left;
                                this.m_Rect.Height = e.Y - m_Rect.Top;
                                break;
                            case 5:
                                int x = m_LastRectPoint.X + e.X - this.m_LastMsPoint.X;
                                int y = m_LastRectPoint.Y + e.Y - this.m_LastMsPoint.Y;
                                this.m_Rect.Location = new Point(x, y);
                                break;
                            default:
                                break;
                        }                }
                    m_Rect.Height = m_Rect.Width * 2;
                    this.pictureBox1.Invalidate();
                    this.pictureBox2.Invalidate();
                }
                else
                {
                    if (rect_left.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeWE;   //向右 
                        this.mouseflag = 4;
                    }
                    else if (rect_right.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeWE;   //向左  
                        this.mouseflag = 6;
                    }
                    else if (rect_top.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeNS;   //向上;
                        this.mouseflag = 8;
                    }
                    else if (rect_bottom.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeNS;   //向下; 
                        this.mouseflag = 2;
                    }
                    else if (m_Rect.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeAll;
                        mouseflag = 5;
                    }
                    else if (rect_rb.Contains(e.Location))
                    {
                        this.Cursor = Cursors.SizeNWSE;
                        mouseflag = -1;
                    }
                    else
                    {
                        this.Cursor = m_oldCursor;
                        mouseflag = 0;
                    }            }
                #endregion
            }        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                m_LastMsPoint = e.Location;
                m_LastRectPoint = m_Rect.Location;
                m_CanMove = this.m_Rect.Contains(e.Location);
            }        private void pictureBox2_Paint(object sender, PaintEventArgs e)
            {
                if (pictureBox1.Image !=null )
                {
                    Rectangle rect = new Rectangle(0, 0, this.m_Rect.Width, this.m_Rect.Height);
                    e.Graphics.DrawImage(this.pictureBox1.Image, rect, this.m_Rect, GraphicsUnit.Pixel);
                }
                
            }    }
      

  6.   

    摄像头呈像控件还是绘不了,把代码写入后就显示不出框框,但是可以在呈现图片的上面绘出框框,这个只是在Picturebox上读取照好的图片然后进行可选。还是希望能找到一种可以在摄像头呈像上绘出框框继续等待答案。。
      

  7.   

    g.DrawImage(newImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);这句后面加上g.DrawRectangle(new Pen(Color.Green,2),1,1,ow-1,oh-1);看看是不是你要的边框效果。每次绘制之前在原图片上加上边框再绘制就行了。
      

  8.   

    做一个UpdateLayeredWidow透明窗口即可。
      

  9.   

    把窗口加到一个控件里挺简单的controls.add(窗口就可以了。)网上也有代码。只不过移动的时候要使用api。
      

  10.   

    http://msdn.microsoft.com/zh-cn/library/microsoft.visualbasic.powerpacks.rectangleshape(v=vs.90).aspx你的工具箱上就有这个现成的控件。
      

  11.   


    vb中基本图形控件,从vs2002就有。运行时进行拖拉,是自己捕获鼠标事件来实现的。难道你弄个透明Panel,就能不编写任何代码而拖拉了?
      

  12.   

    采用自绘吧http://blog.csdn.net/fwj380891124
      

  13.   

    也许有更好的办法。
    但在我的认知中,摄像头的数据是利用驱动直接输出到屏幕的。
    因此,使用任何遮盖性的办法都不是可取的。
    只有从摄像头中取出数据的时候,也就是取出一张张图片的时候,
    在那个上面是可以随意处理的,加文字,画框都没有问题。(亲手做过)所以,要使用DirectShow来做,这样是可以成功的。
      

  14.   

    CSDN 的头像截取,不过是Flash做的。
      

  15.   

    用wpf行不行?这个不是听说样式老嗲的嘛。。
      

  16.   

    直接写一个控件继承自usercontrol
    在onPaint()事件里绘图
    (1)绘图用drawbitmap
    (2)绘矩形什么的用drawrectangle
    再做鼠标事件控制就可以了
      

  17.   

    下面贴出代码:
    C# code    public partial class EditPhoto : Form
        {        private Rectangle m_Rect;
            private Point m_LastMsPoint;
            private Point m_LastRect……
    [/Quote] 这个有点靠谱
      

  18.   


    关键是怎么采用DirectShow来做??其实有些工业摄像机自带的sdk会自带overlay的方法,用这个方法能在上面画东西,其他的不行!!