How to use ColorDialog, openFileDialog and SizeMode.

Create a Simple Picture viewer

What this code should do:

  1. Show the color dialog box. If the user clicks OK.
  2. Change the PictureBox control's background to the color the user chose.
  3. Show the Open File dialog.
  4.  If the user clicks OK, load the picture that the user chose.
  5.  Clear the picture with the clear button.
  6.  Close the form.
  7.  If the user selects the Stretch check box, change the PictureBox's
  8. SizeMode property to "Stretch". If the user clears the check box, change it to "Normal".

               Code: Form1.cs


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundButton_Click(object sender, EventArgs e)
{
// Show the color dialog box. If the user clicks OK, change the
// PictureBox control's background to the color the user chose.
if (colorDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.BackColor = colorDialog1.Color;
}
private void showButton_Click(object sender, EventArgs e)
{
// Show the Open File dialog. If the user clicks OK, load the
// picture that the user chose.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
// Clear the picture.
pictureBox1.Image = null;
}
private void CloseButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// If the user selects the Stretch check box,
// change the PictureBox's
// SizeMode property to "Stretch". If the user clears
// the check box, change it to "Normal".
if (checkBox1.Checked)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
else
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
https://www.maxybyte.com/2017/08/how-to-use-colordialog-openfiledialog.html
Set Background













FormPictureViewer/Form1.Designer.cs


partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.showButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.backgroundButton = new System.Windows.Forms.Button();
this.CloseButton = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.flowLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 85F));
this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.checkBox1, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.flowLayoutPanel1, 1, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 90F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(534, 311);
this.tableLayoutPanel1.TabIndex = 0;
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.tableLayoutPanel1.SetColumnSpan(this.pictureBox1, 2);
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(528, 273);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(3, 282);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(55, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "Adjust";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.showButton);
this.flowLayoutPanel1.Controls.Add(this.clearButton);
this.flowLayoutPanel1.Controls.Add(this.backgroundButton);
this.flowLayoutPanel1.Controls.Add(this.CloseButton);
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
this.flowLayoutPanel1.Location = new System.Drawing.Point(83, 282);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(448, 26);
this.flowLayoutPanel1.TabIndex = 2;
//
// showButton
//
this.showButton.AutoSize = true;
this.showButton.Location = new System.Drawing.Point(356, 3);
this.showButton.Name = "showButton";
this.showButton.Size = new System.Drawing.Size(89, 23);
this.showButton.TabIndex = 0;
this.showButton.Text = "Show a Picture\r\n";
this.showButton.UseVisualStyleBackColor = true;
this.showButton.Click += new System.EventHandler(this.showButton_Click);
//
// clearButton
//
this.clearButton.AutoSize = true;
this.clearButton.Location = new System.Drawing.Point(255, 3);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(95, 23);
this.clearButton.TabIndex = 1;
this.clearButton.Text = "Clear the Picture";
this.clearButton.UseVisualStyleBackColor = true;
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
//
// backgroundButton
//
this.backgroundButton.AutoSize = true;
this.backgroundButton.Location = new System.Drawing.Point(112, 3);
this.backgroundButton.Name = "backgroundButton";
this.backgroundButton.Size = new System.Drawing.Size(137, 23);
this.backgroundButton.TabIndex = 2;
this.backgroundButton.Text = "Set the background color";
this.backgroundButton.UseVisualStyleBackColor = true;
this.backgroundButton.Click += new System.EventHandler(this.backgroundButton_Click);
//
// CloseButton
//
this.CloseButton.AutoSize = true;
this.CloseButton.Location = new System.Drawing.Point(30, 3);
this.CloseButton.Name = "CloseButton";
this.CloseButton.Size = new System.Drawing.Size(76, 23);
this.CloseButton.TabIndex = 3;
this.CloseButton.Text = "Close";
this.CloseButton.UseVisualStyleBackColor = true;
this.CloseButton.Click += new System.EventHandler(this.CloseButton_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
this.openFileDialog1.Filter = "JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All file" +
"s (*.*)|*.*";
this.openFileDialog1.Title = "Select a Picture File";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(534, 311);
this.Controls.Add(this.tableLayoutPanel1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "Tolu Picture Viewer";
this.Load += new System.EventHandler(this.Form1_Load);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button showButton;
private System.Windows.Forms.Button clearButton;
private System.Windows.Forms.Button backgroundButton;
private System.Windows.Forms.Button CloseButton;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ColorDialog colorDialog1;
}

https://www.maxybyte.com/2017/08/how-to-use-colordialog-openfiledialog.html
Adjust Image






No comments:

Post a Comment

Note: only a member of this blog may post a comment.