PictureBox doesn't show up in C# WinForm - c#

I am trying to create a custom error dialog, that has a red x on the left hand side. Below is my code;
using JohnsonControls.FieldBusFDD.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JohnsonControls.FieldBusFDD
{
public class ErrorDialog : Dialog
{
public static bool ShowDialog(string text, string title)
{
Form prompt = new Form();
prompt.Width = 435;
prompt.Height = 122;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = title;
prompt.StartPosition = FormStartPosition.CenterScreen;
PictureBox image = new PictureBox();
image.Image = Resources.red_x;
image.Location = new Point(10, 10);
image.Size = new Size(50, 50);
Label textLabel = new Label() { Left = 60, Top = 10, Width = 350, Text = text };
Button confirmation = new Button() { Text = "Ok", Left = 300, Width = 100, Top = 52 };
confirmation.Click += (sender, e) => { prompt.Close(); };
confirmation.DialogResult = DialogResult.OK;
prompt.Controls.Add(image);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? true : false;
}
}
}
The file is stored in the project resources, and compiles just fine but the image is not displayed.

When the pixel size of the image is too big, the PictureBox only shows the upper-left corner of the image by default.
Try this:
image.SizeMode = PictureBoxSizeMode.Zoom;
From MSDN:
By default, in Normal mode, the Image is positioned in the upper-left corner of the PictureBox, and any part of the image that is too big for the PictureBox is clipped. Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox. Using the Zoom value causes the image to be stretched or shrunk to fit the PictureBox; however, the aspect ratio in the original is maintained.

Try to strecth image on your picture box by:
image.SizeMode = PictureBoxSizeMode.StretchImage;
Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox.

Related

How to create a Windows Form in C# that can be seen only but can not be clicked or brought to focus

I am working on an Accessibility Screen Reader application that will put a rectangle highlighter around the element that is selected on the Focused program. I am able to detect the currently focused AutomationElement, and get its BoundingRectangle property. (How is Spy++ element highlighting working?)
Given a rectangle's topLeftCorner and bottomRightCorner, I am trying to draw a highlighter rectangle using the code below:
internal class HighlighterForm : Form
{
public HighlighterForm(Point topLeftCorner, Point bottomRightCorner, float borderWidth=5){
int length = bottomRightCorner.X - topLeftCorner.X;
int width = bottomRightCorner.Y - topLeftCorner.Y;
this.Text = "Highlighter";
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(topLeftCorner.X, topLeftCorner.Y);
this.TopLevel = true;
this.TopMost = true;
this.Size = new Size(length, width);
// Make the Form background transparent
this.TransparencyKey = Color.Turquoise;
this.BackColor = Color.Turquoise;
this.Paint += (o, e) => {
Graphics g = e.Graphics;
using (Pen selPen = new Pen(Color.Blue, borderWidth*2))
{
g.DrawRectangle(selPen, 0, 0, length, width);
}
};
}
}
And then, I am using the following snippet to create the highlighter:
Point topLeftCorner = new Point(10, 50);
Point bottomRightCorner = new Point(600, 200);
HighlighterForm highlighter = new HighlighterForm(topLeftCorner, bottomRightCorner);
highlighter.ShowDialog();
This can draw a rectangle anywhere on the screen, but the problem is that this form does not let any contents found on an app under the rectangle to be selected. For example, if the currently selected element is a page from Microsoft Word, then the user won't be able to select a paragraph because the rectangle is on top.
How can I make sure the rectangle is there only visually and cannot be interacted with or clicked on?

BackgroundImage shifts when mouse cursor is hovering over the button

Sorry for my english. This is the first time I write a question in this language. I will be glad if you correct my grammatical errors.
I create a changing_button and set its BackgroundImage property. I wanted this image to change when cursor is over this button. I made two pictures for that.
blue_image.png: enter image description here
red_image.png: enter image description here
Normally changing_button should display red_image. But if cursor is over this button, it should display blue_image. The problem is that blue_image shifts when I try to override BackgroundImage property: enter image description here
How can I fix this?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
Image blue_image = Image.FromFile("D://images//blue_image.png");
Image red_image = Image.FromFile("D://images//red_image.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
changing_button = new Button
{
BackgroundImage = red_image,
BackgroundImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.MouseEnter += new System.EventHandler(this.test_button_MouseEnter);
changing_button.MouseLeave += new System.EventHandler(this.test_button_MouseLeave);
main_panel.Controls.Add(changing_button, 0, 0);
Controls.Add(main_panel);
}
void test_button_MouseEnter(object sender, EventArgs e)
{
changing_button.Image = blue_image;
}
void test_button_MouseLeave(object sender, EventArgs e)
{
changing_button.Image = red_image;
}
}
}
Instead of changing the background image, set the initial Image to the red image. BackgroundImage and Image look like they have slightly different positions.
changing_button = new Button
{
Image = red_image,
ImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
Use either BackgroundImage or Image, but not both.

How to remove the black border from the disabled button?

I created a changing_button with a background image. The FlatAppearance.BorderSize property of this button is zero, so normally it displays without a border. But if I make changing_button disabled, it will have a black border:
How can I remove this border?
I guess that the border appears because changing_button is in focus then I change its Enable property. For that reason, I tried to remove focus from the button and set changing_button.TabStop = false, but it didn't help.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
private readonly Image enable_img = Image.FromFile("D://images//enable_img.png");
private readonly Image disable_img = Image.FromFile("D://images//disable_img.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
CreateButton();
Controls.Add(changing_button);
}
private void CreateButton()
{
changing_button = new Button
{
BackgroundImage = enable_img,
BackgroundImageLayout = ImageLayout.Center,
TabIndex = 1,
TabStop = false,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(10, 10, 0, 0),
Location = new Point(40, 40),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.Size = new Size(80, 50);
changing_button.Click += new System.EventHandler(this.Button_Click);
}
void Button_Click(object sender, EventArgs e)
{
changing_button.BackgroundImage = disable_img;
changing_button.Enabled = false;
changing_button.TabStop = false;
}
}
}
enable_img.png:
disable_img.png:
To workaround this, I set the border size to 1.
Then set the border color to be the same color of BackColor. Just change on the click event to match the current state.

How to keep picture box right side of table lay out in window form c#

How to get picture box image at right side in TableLayoutPanel in c#? At present I am getting image next line of label but i need picture box text beside a label in TableLayoutPanel.Here is my code
PictureBox pb = new PictureBox();
pb.ImageLocation = ../imagesDT/answered.gif
tableLayoutPanel1.Controls.Add(pb);
I am trying to keep image beside the label in TableLayoutPanel, but at present getting image below the label.
Try this:
class Form4 : Form {
PictureBox pb = new PictureBox() { BackColor = Color.RosyBrown, Dock = DockStyle.Fill };
Label lb = new Label { Text = "Label", AutoSize = true };
TableLayoutPanel panel = new TableLayoutPanel { Dock = DockStyle.Fill };
public Form4() {
Controls.Add(panel);
panel.Controls.Add(lb, 0, 0);
panel.Controls.Add(pb, 1, 0);
}
}

delete folder after I use the program

I create a dialog program, when I crete a picturebox for any picture of one folder, and, when I close this dialog, I whant to delete the picture box, but, I recive an exception, and this exception tell me that: The file 0.jpg is used by another process
But, so, I have tried to dispose all the picturebox ... I have tried all possible things, in my knowledge, of course...
So, my sample code is that:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace delFolder
{
public partial class FormPictureView : Form
{
private PictureBox pbSel = null;
public FormPictureView()
{
InitializeComponent();
AddPictureBox();
}
private void AddPictureBox()
{
int linha = 0;
int x = 10, y = 10;
string pngOutputPath = #"images\";
string[] files = Directory.GetFiles(pngOutputPath);
for (int i = 0; i < files.Length; i++)
{
if (linha == 2)
{
x = 10;
y += 360;
linha = 0;
}
PictureBox pb = new PictureBox();
pb.Name = string.Format("pb{0}", i);
pb.Size = new Size(236, 321);
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Image = Image.FromFile(string.Format("{0}{1}.jpg", pngOutputPath, i));
pb.BackColor = Color.White;
pb.Click += new EventHandler(pb_Click);
pb.Tag = string.Format("{0}{1}.jpg", pngOutputPath, i);
pb.Location = new System.Drawing.Point(x, y);
panel1.Controls.Add(pb);
x += 280;
linha++;
}
}
private void pb_Click(object sender, EventArgs e)
{
if (pbSel != null)
pbSel.BorderStyle = BorderStyle.None;
PictureBox pb = (PictureBox)sender;
pb.BorderStyle = BorderStyle.FixedSingle;
pbSel = pb;
MessageBox.Show(pb.Tag.ToString());
}
}
}
and, this dialog is a MDIParent of Main form..., and, when I close this dialog, I try to delete the picture box, but, it's impossible :(
How I can solve this ?
You need to Dispose() the Image in the PictureBox.
If you set the ImageLocation property instead of the Image property no lock on the file will be aquired and you will be able to delete the file. However the image will not be available to the PictureBox any more and a "no image" icon will be displayed.
To solve this you could copy the image into memory before deleting it:
string imgPath = string.Format("{0}{1}.jpg", pngOutputPath, i);
// Retrieve image from file
Image img = Image.FromFile(imgPath);
// Create new canvas to paint the picture in
Bitmap tempImg = new Bitmap(img.Width, img.Height);
// Paint image in memory
using (Graphics g = Graphics.FromImage(tempImg))
{
g.DrawImage(img, 0, 0);
}
// Assign image to PictureBox
pb.Image = tempImg;
// Dispose original image and free handles
img.Dispose();
// Delete the original file
File.Delete(imgPath);
Set your yourPictureBox.image to null before deleting the image

Categories