📄 bitmapmaniptestform.cs
字号:
this.topLeftLbl,
this.bottomRightYTextBox,
this.bottomRightXTextBox,
this.topLeftYTextBox,
this.topLeftXTextBox,
this.cropBtn,
this.flipBtn,
this.reverseBtn,
this.label1,
this.thumbnailHeightTextBox,
this.thumbnailWidthTextBox,
this.thumbnailBtn,
this.xLabel,
this.resizeHeightTextBox,
this.resizeWidthTextBox,
this.resizeBtn,
this.scaleFactorTextBox,
this.scaleBtn,
this.rotate270,
this.rotate180Btn,
this.rotate90Btn,
this.imgPicBox,
this.fromUrlBtn,
this.fromFileBtn,
this.inputTextBox,
this.inputLbl});
this.Name = "BitmapManipTestForm";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
this.Text = "Adam Nelson\'s Bitmap Manipulation Class Tester";
this.ResumeLayout(false);
}
#endregion
private void Redraw() {
//Called whenever the user applies an additional modification to the bitmap.
Bitmap workingBitmap = this.modifiedBitmap;
if (this.OverlayBitmap != null) {
//Apply overlay
int alpha;
BitmapManipulator.ImageCornerEnum corner = BitmapManipulator.ImageCornerEnum.BottomRight;
alpha = Int32.Parse(this.alphaTextBox.Text);
corner = (BitmapManipulator.ImageCornerEnum)Enum.Parse(corner.GetType(),
(String)this.overlayPositionCombo.SelectedItem);
workingBitmap = BitmapManipulator.OverlayBitmap(workingBitmap,
this.OverlayBitmap,
alpha,
corner);
}
this.imgPicBox.Image = workingBitmap;
this.imgDimensionsLbl.Text = String.Format("{0:d} x {1:d} pixels",
workingBitmap.Size.Width,
workingBitmap.Size.Height);
this.currentFormatValueLbl.Text = BitmapManipulator.MimeTypeFromImageFormat(workingBitmap.RawFormat);
//Grow the form if the whole picture isn't visible
if (this.Width < this.imgPicBox.Location.X + this.imgPicBox.Width + 25) {
this.Width = this.imgPicBox.Location.X + this.imgPicBox.Width + 25;
}
if (this.Height < this.imgPicBox.Location.Y + this.imgPicBox.Height + 25) {
this.Height = this.imgPicBox.Location.Y + this.imgPicBox.Height + 25;
}
}
private void fromFileBtn_Click(object sender, System.EventArgs e) {
//Load image from file
try {
this.originalBitmap = new Bitmap(this.inputTextBox.Text);
this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error loading file.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void fromUrlBtn_Click(object sender, System.EventArgs e) {
//Load image from URL
try {
this.originalBitmap = BitmapManipulator.GetBitmapFromUri(this.inputTextBox.Text);
this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
Redraw();
} catch (BitmapManipulator.BitmapManipException exp) {
MessageBox.Show(String.Format("Error loading file.\nInner Exception: {0}\nError Message:{1}",
exp.InnerException.GetType().Name,
exp.Message),
"Error");
} catch (Exception exp) {
MessageBox.Show(String.Format("Error loading file.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void rotate90Btn_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = BitmapManipulator.RotateBitmapRight90(this.modifiedBitmap);
Redraw();
}
private void rotate180Btn_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = BitmapManipulator.RotateBitmapRight180(this.modifiedBitmap);
Redraw();
}
private void rotate270_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = BitmapManipulator.RotateBitmapRight270(this.modifiedBitmap);
Redraw();
}
private void scaleBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ScaleBitmap(this.modifiedBitmap,
Double.Parse(this.scaleFactorTextBox.Text));
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void resizeBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ResizeBitmap(this.modifiedBitmap,
Int32.Parse(this.resizeWidthTextBox.Text),
Int32.Parse(this.resizeHeightTextBox.Text));
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void thumbnailBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ThumbnailBitmap(this.modifiedBitmap,
Int32.Parse(this.thumbnailWidthTextBox.Text),
Int32.Parse(this.thumbnailHeightTextBox.Text));
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void reverseBtn_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = BitmapManipulator.ReverseBitmap(this.modifiedBitmap);
Redraw();
}
private void flipBtn_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = BitmapManipulator.FlipBitmap(this.modifiedBitmap);
Redraw();
}
private void cropBtn_Click(object sender, System.EventArgs e) {
try {
int topx, topy, bottomx, bottomy;
topx = Int32.Parse(this.topLeftXTextBox.Text);
topy = Int32.Parse(this.topLeftYTextBox.Text);
bottomx = Int32.Parse(this.bottomRightXTextBox.Text);
bottomy = Int32.Parse(this.bottomRightYTextBox.Text);
this.modifiedBitmap = BitmapManipulator.CropBitmap(this.modifiedBitmap,
new Rectangle(topx, topy,
bottomx-topx, bottomy-topy));
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void overlayBtn_Click(object sender, System.EventArgs e) {
try {
this.OverlayBitmap = BitmapManipulator.GetBitmapFromUri(this.overlayImgUrlTextBox.Text);
Redraw();
} catch (BitmapManipulator.BitmapManipException exp) {
MessageBox.Show(String.Format("Error loading overlay file.\nInner Exception: {0}\nError Message:{1}",
exp.InnerException.GetType().Name,
exp.Message),
"Error");
} catch (Exception exp) {
MessageBox.Show(String.Format("Error loading overlay file.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void revertBtn_Click(object sender, System.EventArgs e) {
this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
this.OverlayBitmap = null;
Redraw();
}
private void jpgBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ConvertBitmapToJpeg(this.modifiedBitmap,
Int32.Parse(this.jpgQualityTextBox.Text));
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void bmpBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap,
ImageFormat.Bmp);
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void gifBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap,
ImageFormat.Gif);
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void tiffBtn_Click(object sender, System.EventArgs e) {
try {
BitmapManipulator.TiffCompressionEnum compress = BitmapManipulator.TiffCompressionEnum.None;
compress = (BitmapManipulator.TiffCompressionEnum)Enum.Parse(compress.GetType(),
(String)this.tiffCompressionCombo.SelectedItem);
this.modifiedBitmap = BitmapManipulator.ConvertBitmapToTiff(this.modifiedBitmap,
compress);
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
private void pngBtn_Click(object sender, System.EventArgs e) {
try {
this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap,
ImageFormat.Png);
Redraw();
} catch (Exception exp) {
MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}",
exp.GetType().Name,
exp.Message),
"Error");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -