How to hide/show the background image of pictureBox? - c#

I have a pictureBox with an image inside.
I want, when i click on a button the image should hide and click again to show the image.
In the pictureBox, using paint event i am drawing some lines.
So if im doing pictureBox1.Refresh(); it will draw the lines. I want that if i click on a button the image will not display on/off.
pictureBox1 = null; or pictureBox1.Image.Dispose(); doesn't work it's showing me big red x with white background.

To hide it:
pictureBox.Visible = false;
To hide/show it in a click event:
void SomeButton_Click(Object sender, EventArgs e)
{
pictureBox.Visible = !pictureBox.Visible;
}

For toggling the image in your PictureBox you can create a 1 pixel bitmap and assign it to the picture box when you want hide your image, then assign your image back again. I am a little unclear of what the second part of your question is asking, any drawing in the picturebox's Paint Event will remain unless you exclude it in the Paint Event based on some Condition. If you want to draw a line in the box an toggle it on/off from a button see my second example.
i.e.
public partial class Form1 : Form
{
Bitmap nullBitmap = new Bitmap(1, 1); // create a 1 pixel bitmap
Bitmap myImage = new Bitmap("Load your Image Here"); // Load your image
bool showImage; // boolean variable so we know what image is assigned
public Form1()
{
InitializeComponent();
pictureBox1.Image = myImage;
showImage = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showImage)
{
pictureBox1.Image = nullBitmap;
showImage = false;
}
else
{
pictureBox1.Image = myImage;
showImage = true;
}
}
}
Second Example
public partial class Form1 : Form
{
bool showLines;
public Form1()
{
InitializeComponent();
showLines = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showLines)
{
showLines = false;
pictureBox1.Invalidate();
}
else
{
showLines = true;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(showLines)
e.Graphics.DrawLine(Pens.Purple, 0, 0, 100, 100);
}
}

picturebox1.BackgroundImage = null

Related

PictureBox Show and Dispose

I want to show a PictureBox on button click, this should be on first click Show PictureBox, and on next click Hide PictureBox, again on third click show PictureBox and similar on fourth click again hide the PictureBox.
I have tried below code but it stuck after dispose of picture.
public bool i;
private void button1_Click(object sender, EventArgs e)
{
if (!i)
{
m1();
i = true;
}
else
{
m2();
i = false;
}
}
public void m1()
{
pictureBox1.Show();
}
public void m2()
{
pictureBox1.Dispose();
}
You don't need to Dispose your PictureBox. Just set the visibility like this:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = !pictureBox1.Visible;
}
pictureBox.Visible = true;
pictureBox.Visible = false;
I assume this is for Winforms? Calling Dispose will destroy the picturebox object in memory so that is why you are having the issue.
Use:
// Show the picture box
pictureBox.Visible = true;
// Hide the picture box
pictureBox.Visible = false;
// Show the picture box
pictureBox.Visible = true;
// Hide the picture box
pictureBox.Visible = false;

how to show icon inside a button windowsForms

I want to add icon inside a button. Here is my code
private void Printbutton_Click(object sender, EventArgs e)
{
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
if (SetupThePrinting())
printDocument1.Print();
}
The problem here is that the icon doesn't appear at first , it appears when I click to the button.
What's wrong here ?
you added the icon in printbutton_click event instead defining it in Form initializecomponents
public Form2()
{
InitializeComponent();
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
}
private void Printbutton_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
printDocument1.Print();
}
Like this
public Form1()
{
InitializeComponent();
// Assign an image to the button.
button1.Image = Image.FromFile("C:\\Yourfolder");
// Align the image and text on the button.
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
button1.FlatStyle = FlatStyle.Flat;
}
private void button1_Click(object sender, EventArgs e)
{
// Your print code.
}

Dynamic click event on PictureBox

I am getting a list of pictures from a directory and storing the filenames in a List<String>. I then loop through each of these and create a PictureBox for each of them, I then add the same click event to each. The controls are in a FlowLayoutPanel
foreach(String file in this._files){
PictureBox box = new PictureBox();
box.Height = 50;
box.Width = 50;
box.ImageLocation = file;
box.SizeMode = PictureBoxSizeMode.Zoom;
box.Click += this.PictureClick;
this.flowLayoutPanel1.Controls.Add(box);
}
private void PictureClick(object sender, EventArgs e){
// how do I get the one that has been clicked and set its border color
}
How do I get the one that has been clicked and set its border color?
sender is the PictureBox that was clicked:
private void PictureClick(object sender, EventArgs e) {
PictureBox oPictureBox = (PictureBox)sender;
// add border, do whatever else you want.
}
The sender parameter is indeed your PictureBox, downcast to object. Access it this way:
var pictureBox = sender as PictureBox;
Drawing a border around it could not be so easy as you will have to either override the OnPaint method of the PictureBox, either handle the Paint event.
You can use this class to draw a black thin border around your image.
public class CustomBorderPictureBox : PictureBox
{
public bool BorderDrawn { get; private set; }
public void ToggleBorder()
{
BorderDrawn = !BorderDrawn;
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (BorderDrawn)
using (var pen = new Pen(Color.Black))
pe.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
}
}

How to draw points on a panel with an image

I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code:
private void drawP_MouseDown(object sender, MouseEventArgs e)
{
if (!drawbool)
{
dStartPoint = e.Location;
drawbool = true;
}
drawP.Invalidate();
}
private void drawP_MouseMove(object sender, MouseEventArgs e)
{
if (drawbool)
{
dStartPoint = e.Location;
drawP.Invalidate();
}
}
private void drawP_MouseUp(object sender, MouseEventArgs e)
{
if (drawbool)
{
drawbool = false;
}
}
private void drawP_Paint_1(object sender, PaintEventArgs e)
{
if (drawbool)
{
int dStartX = dStartPoint.X;
int dStartY = dStartPoint.Y;
e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);
Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
drawP.BackgroundImage = (Image)dPPB;
}
}
Whani's the solution? Thanks in advance.
Check out this great example about drawing on panel: Painting on a Panel.
Later you can save your drawing just by invoking panel's method DrawToBitmap.

How to do a background for a label will be without color?

I want to add a label to my form , and I want it without any color- I want just it's text to be visible, I don't find this option in the label's properties, can anyone help me please?
Do you want to make the label (except for the text) transparent? Windows Forms (I assume WinForms - is this true) doesn't really support transparency. The easiest way, sometimes, is Label's Backcolor to Transparent.
label1.BackColor = System.Drawing.Color.Transparent;
You will run into problems though, as WinForms really doesn't properly support transparency. Otherwise, see here:
http://www.doogal.co.uk/transparent.php
http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
http://www.daniweb.com/code/snippet216425.html
Setting the parent of a usercontrol prevents it from being transparent
Good luck!
If you picture box in the background then use this:
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
Put this code below InitializeComponent(); or in Form_Load Method.
Ref: https://www.c-sharpcorner.com/blogs/how-to-make-a-transparent-label-over-a-picturebox1
You are right. but here is the simplest way for making the back color of the label transparent
In the properties window of that label select Web.. In Web select Transparent
:)
this.label1.BackColor = System.Drawing.Color.Transparent;
Let's view 2 possible cases.
Background is a color.
Double-Click the form background in VS constructor. Then, write this code:
/*
This code will set all your object's background color to the same as the form.
This should be written in the body of <FormName>_Load(object, EventArgs).
*/
Control[] objs = new Control[] { /* your object list, e. g { myLabel, myPicture } */ };
foreach (Control control in objs) {
control.BackColor = Color.Transparent;
// OR
control.BackColor = this.BackColor;
}
Background is a PictureBox.
This is also easy. We just need to make all objects as children of your PictureBox and set it's color to transparent. Code:
/*
This code will set all your object's background to transparent and show the PBox.
This should be written in the body of <FormName>_Load(object, EventArgs)'s foreach loop.
Put everything before it the same as in 1st code fragment.
*/
control.Parent = back;
control.BackColor = Color.Transparent;
Let's see the pictures.
Color
Before
After
PictureBox
Before
After (here foreground were changed, don't mind this)
Generally, labels and textboxes that appear in front of an image is best organized in a panel. When rendering, if labels need to be transparent to an image within the panel, you can switch to image as parent of labels in Form initiation like this:
var oldParent = panel1;
var newParent = pictureBox1;
foreach (var label in oldParent.Controls.OfType<Label>())
{
label.Location = newParent.PointToClient(label.Parent.PointToScreen(label.Location));
label.Parent = newParent;
label.BackColor = Color.Transparent;
}
This uses Graphics.CopyFromScreen so the control needs to be added when it's visable on screen.
public partial class TransparentLabelControl : Label
{
public TransparentLabelControl()
{
this.AutoSize = true;
this.Visible = false;
this.ImageAlign = ContentAlignment.TopLeft;
this.Visible = true;
this.Resize += TransparentLabelControl_Resize;
this.LocationChanged += TransparentLabelControl_LocationChanged;
this.TextChanged += TransparentLabelControl_TextChanged;
this.ParentChanged += TransparentLabelControl_ParentChanged;
}
#region Events
private void TransparentLabelControl_ParentChanged(object sender, EventArgs e)
{
SetTransparent();
if (this.Parent != null)
{
this.Parent.ControlAdded += Parent_ControlAdded;
this.Parent.ControlRemoved += Parent_ControlRemoved;
}
}
private void Parent_ControlRemoved(object sender, ControlEventArgs e)
{
SetTransparent();
}
private void Parent_ControlAdded(object sender, ControlEventArgs e)
{
if (this.Bounds.IntersectsWith(e.Control.Bounds))
{
SetTransparent();
}
}
private void TransparentLabelControl_TextChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_LocationChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_Resize(object sender, EventArgs e)
{
SetTransparent();
}
#endregion
public void SetTransparent()
{
if (this.Parent!= null)
{
this.Visible = false;
this.Image = this.takeComponentScreenShot(this.Parent);
this.Visible = true;
}
}
private Bitmap takeComponentScreenShot(Control control)
{
Rectangle rect = control.RectangleToScreen(this.Bounds);
if (rect.Width == 0 || rect.Height == 0)
{
return null;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
return bmp;
}
}
An easy way to have a label with a picture behind it is to use the Image Property of the label itself. This will print the text over the top of the picture, and enable you to align the image (top/bottom/left/right/centre) as required.picture

Categories

Resources