C# user control's background image - c#

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.

Related

Undraw rectangle on an image

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?

C# Winforms Get a Form's Stretched Background Image

How can I retrieve a form's stretched image? When I use MyForm.BackgroundImage it gives me the original image, but not the stretched image that is being displayed on the form.
If I cannot get the image, can I recreate the resulting image from BackgroundImageLayout = Stretch?
Why I want to do this:
I have a control that does not allow transparency. In order to fake transparency I take the background image and create an image for the section that the control covers. This works well until I set the BackGroundImageLayout to anything other than none.
Any help is greatly appreciated, thanks.
You can retrieve/re-create the stretched form background image like so:
private Bitmap getFormBackgroundImage()
{
Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(this.BackgroundImage,
new Rectangle(0, 0, bmp.Width, bmp.Height));
}
return bmp;
}
Then you can crop a portion of it to use as the child control's background.
Try this :
a) Set the background image of the form (Base Control).
this.BackgroundImage = <image>;
b) Create a child control and drop it over over your base control.
c) Set the dock of your child control to Fill.
this.childControl.Dock = DockStyle.Fill;
d) In the base control constructor , set the background image for the child control as the image of the base control. Like this:
childControl.BackgroundImage = this.BackgroundImage;
e) Set the background image layout of the child control to strech.
childControl.BackgroundImageLayout = ImageLayout.Stretch;
This would make your child control appear as transparent. Hope it solves your problem.

Get selected portion of image in picturebox in another picturebox

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.

Overlay of two images - Control which part is in the foreground with mouse coursor

I need to be able to compare two different Images in the following way:
The images are displayed on top of each other so first there is only one image visible. When hovering with the mouse coursor over the image this defines the X-position of a vertical line which is splitting the image, displaying part of the first image left to the line and the rest of the second image right to the line.
This basically should be used as a quality comparison for two images with identical contents.
Here is a picture that hopefully makes my intentions clear:
you can use splitter control. splitter one side you put one picture control another side put other picture control
I was able to resolve this by using a SplitContainer and custom drawing. As described in the comment of Vasanthakumar's answer purely using a picturebox is not enough as you will not be able to align the right image at the same starting point than the left image (top left of the form).
What I did was the following:
pictureBox 1 displays its image normally
the Image in pictureBox 2 is drawn on every move of the splitter (this.splitContainer1.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.splitContainer1_SplitterMoved); with a custom subset of the Image to be displayed.
This effectively allows to generate the overlay I need.
Part of my implementation showing the drawing logic:
Bitmap bmp = new Bitmap(gImg2.Width, gImg2.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(gImg2, 0, 0, new Rectangle(e.SplitX, 0, gImg2.Width - e.SplitX, gImg2.Height), GraphicsUnit.Pixel);
}
pictureBox2.Image = bmp;

Render a section of an image to a Bitmap C# Winforms

I'm working on a Map Editor for an XNA game I'm designing in my free time. The pieces of art used in the map are stored on a single texture and rectangles are stored with coordinates and widths etc.
In the winforms application I can add segments by selecting the segment I want from a listbox, which is populated from the array of possible segments.
Problem is I would like to be able to show a preview of the segment selected and, since it is stored on a common texture, I cant simply set a picturebox to display the image.
Is there anyway of using the rectangle information (.x, .y, .width, .height) to display only the section of the image in a picturebox, or to blit the section to a bitmap and display that?
Many Thanks
Michael Allen
You probably want to look into the GDI library. Using the Image or Bitmap object and the Graphics.DrawImage() together will get what you're looking for.
private void DrawImageRectRect(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(100, 100, 450, 150);
// Create rectangle for source image.
Rectangle srcRect = new Rectangle(50, 50, 150, 150);
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, srcRect, units);
}
You also might be interested in using XNA within your WinForm instead of using PictureBoxes and GDI. It's not 100% supported yet, but a tutorial on that can be found here.
You can use Graphics.DrawImage() and that will accept a Rectangle.

Categories