📄 frmshow.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
namespace SmartPhoneAlbum
{
/// <summary>
/// Summary description for frmShow.
/// </summary>
public class frmShow : System.Windows.Forms.Form
{
private Form parentForm;//Form parentForm
private PictureBox pictureBox1;//to display picture
private Bitmap imgBuffer=null;//buffer bitmap
private Image imgLarge;//save large image
private Bitmap imgGrey = null;//mono Bitmap
private Bitmap imgBuff;//image buffer
private Bitmap imgPan;//image pan
private MainMenu mainMenu1;
private MenuItem menuItem1;
private MenuItem menuItem2;
private MenuItem menuItem3;
private MenuItem menuItem4;
private MenuItem menuItem5;
private bool bTurn = false;
private bool bGrey = false;
private MenuItem menuItem6;
public string strImgFile;//file name
private Bitmap histogramBMP;//hostogram
private int[] histogram;
private int xOffset, yOffset,xDif,yDif;//to scrolling picture this variables required
Rectangle rectBuff;
private MenuItem menuItem7;
private MenuItem menuItem8;
public frmShow(Form parentForm)
{
histogram = new int[256];
InitializeComponent();
this.parentForm = parentForm;
}
#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()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Size = new System.Drawing.Size(176, 179);
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);
//
// menuItem1
//
this.menuItem1.Text = "Back";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.MenuItems.Add(this.menuItem4);
this.menuItem2.MenuItems.Add(this.menuItem5);
this.menuItem2.MenuItems.Add(this.menuItem6);
this.menuItem2.MenuItems.Add(this.menuItem7);
this.menuItem2.Text = "Menu";
//
// menuItem3
//
this.menuItem3.Text = "Center";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Text = "Stretch";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Text = "Normal";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Text = "Mono-Colored";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Text = "Histogram";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// frmShow
//
this.ClientSize = new System.Drawing.Size(176, 180);
this.Controls.Add(this.pictureBox1);
this.Menu = this.mainMenu1;
this.Load += new System.EventHandler(this.frmShow_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmShow_Paint);
this.Closing += new System.ComponentModel.CancelEventHandler(this.frmShow_Closing);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmShow_KeyDown);
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
/// <summary>
/// to open picture file from strImgFile
/// </summary>
private void frmShow_Load(object sender, EventArgs e)
{
imgLarge = new Bitmap(strImgFile);//picture is loaded from strImgFile which assigned by frmMain
pictureBox1.Image = imgLarge;
xOffset = 0; yOffset = 0;
imgPan = new Bitmap(imgLarge);
xDif = imgLarge.Width - pictureBox1.Width;
yDif = imgLarge.Height - pictureBox1.Height;
if (xDif < 0)
xDif = 0;
if (yDif < 0)
yDif = 0;
}
private void menuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void menuItem3_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
}
private void menuItem4_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
}
private void menuItem5_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
}
private void frmShow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
/// <summary>
/// gary<->color
/// </summary>
private void TurnToGrey()
{
Cursor.Current = Cursors.WaitCursor;
Color pixel;
int t;
if (imgGrey == null)
imgGrey = new Bitmap(pictureBox1.Image);
if(imgBuffer==null)
imgBuffer = new Bitmap(imgGrey);
if (!bTurn)
{
if (!bGrey)
{
for (int y = 0; y < pictureBox1.Image.Height; y++)
{
for (int x = 0; x < pictureBox1.Image.Width; x++)
{
pixel = imgGrey.GetPixel(x, y);
t = (pixel.R + pixel.G + pixel.B) / 3;
imgGrey.SetPixel(x, y, Color.FromArgb(t, t, t));
}
}
bGrey = true;
}
pictureBox1.Image = imgGrey;
bTurn = true;
}
else
{
pictureBox1.Image = imgBuffer;
bTurn = false;
}
Cursor.Current = Cursors.Default;
}
/// <summary>
/// show histo gram
/// </summary>
private void show_histo()
{
Color pixel;
// Cursor.Current = Cursors.WaitCursor;
if (imgGrey == null)
imgGrey = new Bitmap(pictureBox1.Image);
for (int y = 0; y < pictureBox1.Image.Height; y++)
{
for (int x = 0; x < pictureBox1.Image.Width; x++)
{
pixel = imgGrey.GetPixel(x, y);
int grayLevel = (pixel.R + pixel.G + pixel.B) / 3;
histogram[grayLevel]++;
}
}
pictureBox1.Image = imgGrey;
histogramBMP = new Bitmap(176, 180);
for (int i = 0; i < 176; i++)
{
for (int j = 0; j < 180; j++)
{
histogramBMP.SetPixel(i, j, Color.White);
}
}
Graphics g = Graphics.FromImage(histogramBMP);
Pen myPen = new Pen(Color.DarkGray);
g.DrawLine(myPen, 4, 150,100, 150); //X-axis
g.DrawLine(myPen, 4, 150,4, 6); //Y-axis
float totalPoints = pictureBox1.Image.Height * pictureBox1.Image.Width;
for (int i = 0; i < 150; i++)
{
Pen myPen2 = new Pen(Color.Red);
g.DrawLine(myPen2, 4 + i, 150, 4 + i, 150 - histogram[i]/10);
/*
if (i > 0 && i % 20 == 0)
{
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new Font("Arial",5);
String drawString = i.ToString();
//g.DrawString(drawString, drawFont, drawBrush, 4 ,2 * i, 41);
// g.DrawString(drawString, drawFont, drawBrush, 4, 2 * i, 41);
}*/
}
pictureBox1.Image = histogramBMP;
}
/// <summary>
/// save gray picture
/// </summary>
private void save_gray()
{
Color pixel;
for(int i=0;i<pictureBox1.Image.Height;i++)
{
for(int j=0;j<pictureBox1.Image.Width;j++)
{
pixel = imgGrey.GetPixel(i, j);
int grayLevel = (pixel.R+pixel.G+pixel.B) /3;
// int grayLevel = 3;
}
}
}
/// <summary>
///
/// </summary>
private void menuItem6_Click(object sender, EventArgs e)
{
if (!bTurn)
{
menuItem6.Text = "Multi-Colored";
TurnToGrey();
imgPan = new Bitmap(imgGrey);
}
else
{
menuItem6.Text = "Mono-Colored";
TurnToGrey();
imgPan = new Bitmap(imgLarge);
}
}
/// <summary>
/// key event
/// </summary>
private void frmShow_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.F1))
{
// left softkey
// Not handled when menu is present.
}
if ((e.KeyCode == System.Windows.Forms.Keys.F2))
{
// right softkey
// Not handled when menu is present.
}
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
if (yDif != 0)
{
if (yOffset >= 0 && yOffset <= yDif)
yOffset--;
else if (yOffset < 0)
yOffset = 0;
Invalidate();
}
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
// dpad-down
if (yOffset >= 0 && yOffset < yDif)
yOffset++;
else if (yOffset > yDif)
yOffset = 0;
Invalidate();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
// dpad-left
if (xDif != 0)
{
if (xOffset >= 0 && xOffset <= xDif)
xOffset--;
else if (xOffset < 0)
xOffset = 0;
}
Invalidate();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
// dpad-right
if (xOffset >= 0 && xOffset < xDif)
xOffset++;
else if (xOffset > xDif)
xOffset = 0;
Invalidate();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
// dpad-action
}
if ((e.KeyCode == System.Windows.Forms.Keys.D1))
{
// num pad 1
}
if ((e.KeyCode == System.Windows.Forms.Keys.D2))
{
// num pad 2
}
if ((e.KeyCode == System.Windows.Forms.Keys.D3))
{
// num pad 3
}
if ((e.KeyCode == System.Windows.Forms.Keys.D4))
{
// num pad 4
}
if ((e.KeyCode == System.Windows.Forms.Keys.D5))
{
// num pad 5
}
if ((e.KeyCode == System.Windows.Forms.Keys.D6))
{
// num pad 6
}
if ((e.KeyCode == System.Windows.Forms.Keys.D7))
{
// num pad 7
}
if ((e.KeyCode == System.Windows.Forms.Keys.D8))
{
// num pad 8
}
if ((e.KeyCode == System.Windows.Forms.Keys.D9))
{
// num pad 9
}
if ((e.KeyCode == System.Windows.Forms.Keys.F8))
{
// num pad *
}
if ((e.KeyCode == System.Windows.Forms.Keys.D0))
{
// num pad 0
}
if ((e.KeyCode == System.Windows.Forms.Keys.F9))
{
// num pad #
}
}
/// <summary>
/// paint
/// </summary>
private void frmShow_Paint(object sender, PaintEventArgs e)
{
if (menuItem7.Checked == true)
{
if (imgBuff == null)
{
imgBuff = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
}
//the picture is buffered for scrolling
using (Graphics g = Graphics.FromImage(imgBuff))
{
g.Clear(Color.White);
rectBuff = new Rectangle(xOffset, yOffset, pictureBox1.Image.Width, pictureBox1.Image.Height);
g.DrawImage(imgPan, 0, 0, rectBuff, GraphicsUnit.Pixel);
}
pictureBox1.Image = imgBuff;
}
}
/// <summary>
/// histogram
/// </summary>
private void menuItem7_Click(object sender, EventArgs e)
{
/*
if (menuItem7.Checked == false)
{
menuItem7.Checked = true;
}
else if (menuItem7.Checked == true)
{
menuItem7.Checked = false;
}*/
show_histo();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -