C# Winforms Get a Form's Stretched Background Image - c#

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.

Related

C# PictureBox image dont draw complete

i create a panel and inside this panel i create a picture box. this have no default image. after a function i load a image und put it into this picturebox. the image was bigger as the panel and i want to scroll the image but the image draw only in the first area to see and if i scroll the image dont draw complete. how i can fix this ?
Image TreeImg = new Bitmap(imgPath);
maletreePictureBox.Width = TreeImg.Width;
maletreePictureBox.Height = TreeImg.Height;
maletreePictureBox.Image = TreeImg;
actionPanel.AutoScrollMinSize = new Size(TreeImg.Width, driverTreeImg.Height);
this is the result
Set the .Dock property of the maletreePictureBox to Fill.
Also it looks like your AutoScrollMinSize Height property is incorrect (driverTreeImg.Height instead of TreeImg.Height)

C# user control's background image

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.

Align button background image

I have a button in my WinForms app and I added an image and text to it. I aligned the text to right and wanted to align the Background image to left but found out that it is not possible.
Is there any way to do that?
I have also tried to set just Image on the button but that couldn't be resized in the Button Properties.
May someone help me solve this out? Thanks so much.
In case that it is not possible I would have to resize every image in mspaint.
This is the result (as Background):
I need the BackgroundImage align to left.
This is result as Image when using align (not possible to resize)
Use Image property to set your image (make sure it fits button height, you can change image size if you will open it from project resources folder)
Set ImageAlign to MiddleLeft
Set TextAlign to MiddleRight
Do not change anything else. I.e. TextImageRelation should be Overlay. Result:
Set these properties of Button.
ImageAlign to MiddleRight
TextImageRelation to ImageBeforeText
TextAlign as MiddleCenter
To have it resized on Button. See below:
Bitmap image = Bitmap.FromFile(oFile) as Bitmap;
Bitmap resized = new Bitmap(image, new Size(30, 30));
button1.Image = resized;
button1.Text = "Button";
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;
You can use the Image property instead of the BackgroundImage. You can set the align later using the ImageAlign property.

c#: Getting the image behind a control

I have c# form which have several controls on it, part of the controls are located one on another. I want a function that will take for input a control from the form and will return the image that has to be behind the control. for ex: if the form has backgroundimage and contains a button on it - if I'll run this function I'll got the part of backgroundimage that located behind the button. any Idea - and code?
H-E-L-P!!!
It may be simpler to use an image map http://www.echoecho.com/htmllinks07.htm
You can create your links using Page.ClientScript.GetCallback...
Edit: Winforms solution
This takes the background image of 'this' which you can change to your form object and copies the whatever is at the same position and size of your button to a bitmap object.
Graphics objGraphics = Graphics.FromImage(this.BackgroundImage);
Bitmap objBitmap = new Bitmap(button1.Size.Width, button1.Size.Height);
objGraphics.DrawImage(bitmap, new Point(0, 0), new Rectangle(button1.Location, button1.Size), GraphicsUnit.Pixel);

Panel of controls over content in the form

Does WinForms have support for this? Let's say I want to show a panel containing ListView made of some results. It is shown by clicking on a button in a form corner, and the panel is shown inside the form, above all controls, preferably with shadow in the back to make it more attractive.
I want this panel to be always inside the form, as a part of it. One solution that comes to my mind is to create a border-less form with that panel, and to make it in sync with original form.
Is there maybe some other way that I'm missing?
Here's a crude method that will get you started:
public Panel CreateFloatingPanel(Panel originalPanel)
{
Bitmap bmp = new Bitmap(originalPanel.Width,
originalPanel.Height);
Rectangle rect = new Rectangle(0, 0,
bmp.Width, bmp.Height);
originalPanel.DrawToBitmap(bmp, rect);
foreach (Control ctrl in originalPanel.Controls)
{
ctrl.Visible = false;
}
using (Graphics g = Graphics.FromHwnd(originalPanel.Handle))
{
g.DrawImage(bmp, 0, 0);
bmp.Dispose();
SolidBrush brush =
new SolidBrush(Color.FromArgb(128, Color.Gray));
g.FillRectangle(brush, rect);
brush.Dispose();
}
Panel floater = new Panel();
floater.Size = originalPanel.Size;
floater.Left = originalPanel.Left - 50;
floater.Top = originalPanel.Top - 50;
this.Controls.Add(floater);
floater.BringToFront();
return floater;
}
This method takes a panel with some controls on it, draws the panel with all its controls onto a temporary bitmap, makes all the controls invisible, draws the temporary bitmap onto the panel, then adds a semi-transparent Gray layer onto the panel. The method then creates a new panel and floats it over the original panel, above and to the left, and then returns it. This basically makes the new panel a sort of modal popup window, kind of like how web pages do it sometimes.
To use this on your form, put all the controls you want to be grayed-out underneath onto a panel, then have your button's Click event do something like this:
Panel floater = CreateFloatingPanel(panel1);
floater.BackColor = Color.White;
ListView lv = new ListView();
floater.Controls.Add(lv);
To undo the effect, you would just remove the floating panel from the form's Controls collection, then make visible again all the controls on the original panel, and then Refresh or Invalidate the panel to get rid of the grayed-out stuff. The simple Invalidate will work because the grayed-out effect does not persist - to get that you'd have to make this all a bit more complicated. But this should get you started, at least.
Could you just create a hidden Panel at the back of the Form, and then make it visible, and then bring it to front?

Categories