Delete button

This commit is contained in:
2015-12-26 13:36:58 +01:00
parent 60e39ddee3
commit 58bc3adc26
2 changed files with 49 additions and 10 deletions

View File

@@ -33,6 +33,7 @@
this.btnPrev = new System.Windows.Forms.Button(); this.btnPrev = new System.Windows.Forms.Button();
this.btnSelectInExplorer = new System.Windows.Forms.Button(); this.btnSelectInExplorer = new System.Windows.Forms.Button();
this.picImageView = new ImageView.Controls.CtrImageViewer(); this.picImageView = new ImageView.Controls.CtrImageViewer();
this.btnDelete = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.picImageView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.picImageView)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@@ -82,11 +83,23 @@
this.picImageView.TabIndex = 0; this.picImageView.TabIndex = 0;
this.picImageView.TabStop = false; this.picImageView.TabStop = false;
// //
// btnDelete
//
this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnDelete.Location = new System.Drawing.Point(194, 521);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(75, 23);
this.btnDelete.TabIndex = 4;
this.btnDelete.Text = "Delete";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// FrmImageView // FrmImageView
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(663, 550); this.ClientSize = new System.Drawing.Size(663, 550);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnSelectInExplorer); this.Controls.Add(this.btnSelectInExplorer);
this.Controls.Add(this.btnPrev); this.Controls.Add(this.btnPrev);
this.Controls.Add(this.btnNext); this.Controls.Add(this.btnNext);
@@ -105,5 +118,6 @@
private System.Windows.Forms.Button btnNext; private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Button btnPrev; private System.Windows.Forms.Button btnPrev;
private System.Windows.Forms.Button btnSelectInExplorer; private System.Windows.Forms.Button btnSelectInExplorer;
private System.Windows.Forms.Button btnDelete;
} }
} }

View File

@@ -39,10 +39,13 @@ namespace ImageView
Image image; Image image;
try try
{ {
image = Bitmap.FromFile(_imagePath); using (var bmpTemp = new Bitmap(_imagePath))
{
FixImageOrientation(bmpTemp);
image = new Bitmap(bmpTemp);
}
} }
catch (Exception) { return false; } catch (Exception) { return false; }
FixImageOrientation(image);
picImageView.ImageShow = image; picImageView.ImageShow = image;
Text = Path.GetFileName(_imagePath); Text = Path.GetFileName(_imagePath);
return true; return true;
@@ -70,25 +73,25 @@ namespace ImageView
return files[idx]; return files[idx];
} }
private void NextImage() private bool NextImage()
{ {
string filename = _imagePath; string filename = _imagePath;
do do
{ {
filename = NextFile(filename); filename = NextFile(filename);
if (filename == null) { break; } if (filename == null) { return false; }
if (SetImage(filename)) { break; } if (SetImage(filename)) { return true; }
} while (true); } while (true);
} }
private void PrevImage() private bool PrevImage()
{ {
string filename = _imagePath; string filename = _imagePath;
do do
{ {
filename = PrevFile(filename); filename = PrevFile(filename);
if (filename == null) { break; } if (filename == null) { return false; }
if (SetImage(filename)) { break; } if (SetImage(filename)) { return true; }
} while (true); } while (true);
} }
@@ -110,8 +113,12 @@ namespace ImageView
public static short GetExifOrientation(Image photo) public static short GetExifOrientation(Image photo)
{ {
const int orientationIndex = 0x0112; const int orientationIndex = 0x0112;
PropertyItem prop = photo.GetPropertyItem(orientationIndex); try
return BitConverter.ToInt16(prop.Value, 0); {
PropertyItem prop = photo.GetPropertyItem(orientationIndex);
return BitConverter.ToInt16(prop.Value, 0);
}
catch (Exception) { return 0; }
} }
public static void FixImageOrientation(Image image) public static void FixImageOrientation(Image image)
@@ -131,5 +138,23 @@ namespace ImageView
} }
} }
private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to delete this image?", "", MessageBoxButtons.YesNo);
if (result != DialogResult.Yes)
{
return;
}
string fileToDelete = _imagePath;
if (NextImage() == false)
{
if (PrevImage() == false)
{
return;
}
}
File.Delete(fileToDelete);
}
} }
} }