I have a custom control that displays an image. I'm using a custom control because I've implemented zooming functionality and scrollbars.
I'm draw a rectangle on the image by doing:
if (drawRect)
{
var g = Graphics.FromImage(Image);
g.DrawRectangle(Pens.Red, rect);
}
The drawing of the rectangle is toggled by a button. However, when I toggle off, the rectangle it's still being drawn on the image.
I'm assuming that once something is drawn on the image's surface, it stays there.
Is there a way to "undraw" something or do I need to implement a second "transparent" image that will act as an overlay?
Related
I have a custom control that I'm inheriting from Control. I need to display an icon so I'm using the available properties (BackgroundImage and BackgroundImageLayout).
I select an image and set the layout to None. However, I don#t want that the image is draw in the top-left corner of the control, but in another location that I specify.
Is this possible?
Try something like this inside your OnPaint();
Image myBackgroundImage = Image.FromFile("xyz.jpg");
e.Graphics.DrawImage(myBackgroundImage, myX, myY, new Rectangle(0, 0, myBackgroundImage.Width, myBackgroundImage.Height), GraphicsUnit.Pixel);
You're telling the Graphics object to draw your image at the specific location of myX and myY. The rectangle is the portion of the image to draw, in this case the whole image.
I have a picturebox with image, in which I have drawn a rectangular selection. I want to get the selected portion of image in another picturebox. How can I get that? Please help.
Suppose the rect is the Rectangle which you passed in the Graphics.DrawRectangle is calculated in your the coordinates of your pictureBox. You can use RectangleToScreen and RectangleToClient to get that portion in another pictureBox like this:
Rectangle portion = pictureBox2.RectagleToClient(pictureBox1.RectangleToScreen(rect));
//portion is the Rectangle calculated in the coordinates of pictureBox2.
Use coordinates of the selection to create Rectangle rectangle then:
Bitmap sourceBitmap = new Bitmap(pictureBoxImage);
Bitmap croppedBitmap = sourceBitmap.Clone(rectangle, sourceBitmap.PixelFormat);
after that you may use croppedBitmap in another picturebox. Don't forget to dispose unused images. That's all.
I'm trying to create a semi-transparent user control button whose background opacity changes from 30% black to 70% black on mouseEnter. I can successfully paint the button 30% black but I can't go from 70% to 30%. It's as if every time OnPaint() is called, it paints over what was already there instead of starting from scratch. I use a subclassed Panel as the user control's background.
Here's my OnPaint method for the subclassed Panel:
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
Brush brush = new SolidBrush(Color.FromArgb(Alpha, Color.Black)); // alpha is set by me
g.FillRectangle(brush, rect);
g.Dispose();
I've played with g.Clear() and g.Restore() but haven't had any luck. Any ideas?
Thanks!
I'm not sure how Windows handles transparency in that case. You should try invalidating the rectangle on the window below your window, if that window is repainted, the background might look just like you want.
I ended up solving this by swapping 30% opaque black and 70% opaque black assets for the background image. I also set the button's BackColor to Color.Transparent.
How to draw a rectangle on picturebox and save selected part as .jpg.
Well i know how to draw a rectange on picturebox, but i don't know how part of picture inside this rectangle can be saved as for example new .jpg..
If your image is always perfectly fited in picture box, you can create Graphics from Image class
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx
And after use Graphics http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimageunscaledandclipped.aspx DrawImageUnscaledAndCliped method to clip your image in memory. After this you can save that Image to file.
If image is not fitted so you use SizeMode picturebox property, you need to calculate a proportion of drawn rectangle in regard of picture box rectangle and after apply that proportion to image real rectangle.
I have created a custom control (the control is used for drag and drop) and I want to add focus and selected events to the control. Both need to be visually distinct. So I plan to implement a windows style for both of these events. For focus I have the control drawing a solid and a dotted line around the control using the following code in the Paint event.
if (Image != null)
{
if (ContainsFocus)
{
// Draw a dotted line inside the client rectangle
Rectangle insideRectangle = ClientRectangle;
insideRectangle.Inflate(-2, -2);
insideRectangle.Width--;
insideRectangle.Height--;
Pen p = new Pen(Color.Black, 1);
p.DashStyle = DashStyle.Dot;
g.DrawRectangle(p, insideRectangle);
// Draw a solid line on the edge of the client rectangle
Rectangle outsideRectangle = ClientRectangle;
outsideRectangle.Width--;
outsideRectangle.Height--;
p.DashStyle = DashStyle.Solid;
g.DrawRectangle(p, outsideRectangle);
Color transparentLightBlue = Color.FromArgb(100, Color.LightBlue);
Brush solidBrush = new SolidBrush(transparentLightBlue);
g.FillRectangle(solidBrush, ClientRectangle);
}
}
For the Focus event I want just the image to be highlighted (similar to windows explorer). My first attempt at this was to add the following code.
Color transparentLightBlue = Color.FromArgb(100, Color.LightBlue);
Brush solidBrush = new SolidBrush(transparentLightBlue);
g.FillRectangle(solidBrush, ClientRectangle);
This works filling in the rectangle however I would like to just highlight the image itself instead of the entire rectangle. I've had the idea of using two different images, however the image is supplied to me and I'm not storing them.
So my question: How is the best way to get just the image of the control that has focus to highlight?
Thank you in advance!
since your image is not transparent you could overlay it with a transparent highlight color. something similar to this.