문제

How do I highlight specific part of the bitmap that selected by mouse? The highlighted part will be showed in different color. Anyone can help? Thanks a lot.

The highlight specific part of bitmap are something like the picture shown

올바른 솔루션이 없습니다

다른 팁

The main thing about "highlighting" either on images or controls is that you need to gain access to a graphics object. To get one for a bitmap you can do:

g = Graphics.FromImage(myBitmap);
g.DrawRectangle(Pens.Red, 0, 0, 100, 100);
g.Dispose();

Here is some code that uses the same principle but with a picturebox control in order to work with mouse movements in C#.

private bool expanding;
private bool selectActive = false;
private Point selectStart;
private Point selectPosition;
private bool selecting;
private bool selectionExists;
private bool manualFinding;


private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (!selectActive)
    {
        return;
    }
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        selectStart = e.Location;
        selecting = true;
    }

}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (selecting)
    {
        selectPosition = e.Location;
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    int x = Math.Min(selectStart.X, selectPosition.X);
    int y = Math.Min(selectStart.Y, selectPosition.Y);
    int w = Math.Abs(selectStart.X - selectPosition.X);
    int h = Math.Abs(selectStart.Y - selectPosition.Y);
    if (selectionExists || selecting)
    {
        e.Graphics.DrawRectangle(Pens.Red, x, y, w, h);
    }

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (!selecting)
    {
        return;

    }

    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        selecting = false;
        selectionExists = true;
    }

}

Here is a different example of how you might create a control that can draw an image, but then where you can draw arbitrary stuff on top of the image during each invocation of the paint event:

public class ImageControl : Control
{
    [Description("The base image for this control to render.")]
    public Bitmap Image { get; set; }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // base.OnPaintBackground(pevent);
    }

    /// <summary>
    /// Override paint so that it uses your glow regardless of when it is instructed to draw
    /// </summary>
    /// <param name="pevent"></param>
    protected override void OnPaint(PaintEventArgs pevent)
    {
        pevent.Graphics.DrawImage(Image, 0, 0, Width, Height);
        pevent.Graphics.DrawLine(Pens.Blue, 0, 0, 100, 100);
    }

}

Complementing Ted´s answer, here are my two cents.

If you want to highlight certain area inside a bitmap I would draw a semi-transparent rectangle. You can achieve that creating a SolidBrush() with a RGB color using the alpha channel.

    using (Graphics graphics = Graphics.FromImage(waveBitmap))
    {
        // Paint transparent black mask
        graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 0, 0, 100, 100);
    }

So, with this code Color.FromArgb(100, 0, 0, 0) you create a black color (RGB 0,0,0) with alpha value (transparency) of 100 (values goes from 0 to 255, where 0 is fully transparent and 255 is opaque).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top