一个改变图片大小的东东
2007-04-08 16:29
239 查看
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace BmpOper
...{
public partial class Form1 : Form
...{
string picPath;
public Form1()
...{
InitializeComponent();
picPath = "";
}
private void open_Click(object sender, EventArgs e)
...{
//open source picture and save the path of it to "picPath":
if (openPicDialog.ShowDialog()== DialogResult.OK)
...{
picPath=openPicDialog.FileName;
4000
showResult.ImageLocation = picPath; //preview
}
}
private void save_Click(object sender, EventArgs e)
...{
//get Current directory and setup the name of Thumbnail:
string savePath = picPath.Substring(0, picPath.LastIndexOf(".")) + "Thum.jpg";//get the dir
//Transfer the Function :
GenerateThum(savePath);
}
private void GenerateThum(string savePath) //Generate the Thumbnail of open picture
...{
if (picPath != "")
...{
Validate the TextBox#region Validate the TextBox
string tempW = inputWidth.Text;
string tempH = inputHeigh.Text;
if (!Regex.IsMatch(tempW, @"^[0-9]+$") && Regex.IsMatch(tempH, @"^[0-9]+$"))
...{
MessageBox.Show("Error,pleae input a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
inputWidth.Focus();
}
#endregion
//get the width&height&savepath
int width = Int32.Parse(inputWidth.Text);
int height = Int32.Parse(inputHeigh.Text);
string ThumbPath = savePath;
//Generate
Bitmap newBmp = new Bitmap(picPath);
Image Thumbnail = newBmp.GetThumbnailImage(width,height, null, IntPtr.Zero);
//Save
Thumbnail.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private void saveAs_Click(object sender, EventArgs e) //save as
...{
if (savePicDialog.ShowDialog() == DialogResult.OK)
...{
string savePath = savePicDialog.FileName;
GenerateThum(savePath);
}
}
}
1871f
}
/**//*
* Crazy7
* @ 2007/04/08 pm
* A program for generate thumbnail of a picture.
*/
Form1.Designer.cs:
namespace BmpOper
...{
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);
}
Windows Form Designer generated code#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.open = new System.Windows.Forms.Button();
this.openPicDialog = new System.Windows.Forms.OpenFileDialog();
this.showResult = new System.Windows.Forms.PictureBox();
this.save = new System.Windows.Forms.Button();
this.inputHeigh = new System.Windows.Forms.TextBox();
this.inputWidth = new System.Windows.Forms.TextBox();
this.savePicDialog = new System.Windows.Forms.SaveFileDialog();
this.saveAs = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.showResult)).BeginInit();
this.SuspendLayout();
//
// open
//
this.open.Location = new System.Drawing.Point(192, 89);
this.open.Name = "open";
this.open.Size = new System.Drawing.Size(94, 24);
this.open.TabIndex = 0;
this.open.Text = "Select Picture";
this.open.UseVisualStyleBackColor = true;
this.open.Click += new System.EventHandler(this.open_Click);
//
// openPicDialog
//
this.openPicDialog.Filter = "jpg|*.jpg|bmp|*.bmp";
//
// showResult
//
this.showResult.Location = new System.Drawing.Point(12, 89);
this.showResult.Name = "showResult";
this.showResult.Size = new System.Drawing.Size(174, 161);
this.showResult.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.showResult.TabIndex = 1;
this.showResult.TabStop = false;
//
// save
//
this.save.Location = new System.Drawing.Point(192, 159);
this.save.Name = "save";
this.save.Size = new System.Drawing.Size(94, 24);
this.save.TabIndex = 2;
this.save.Text = "Save Picutre";
this.save.UseVisualStyleBackColor = true;
this.save.Click += new System.EventHandler(this.save_Click);
//
// inputHeigh
//
this.inputHeigh.Location = new System.Drawing.Point(91, 39);
this.inputHeigh.Name = "inputHeigh";
this.inputHeigh.Size = new System.Drawing.Size(41, 23);
this.inputHeigh.TabIndex = 3;
this.inputHeigh.Text = "75";
//
// inputWidth
//
this.inputWidth.Location = new System.Drawing.Point(22, 39);
this.inputWidth.Name = "inputWidth";
this.inputWidth.Size = new System.Drawing.Size(42, 23);
this.inputWidth.TabIndex = 4;
this.inputWidth.Text = "100";
//
// savePicDialog
//
this.savePicDialog.Filter = "jpg|*.jpg";
//
// saveAs
//
this.saveAs.Location = new System.Drawing.Point(192, 227);
this.saveAs.Name = "saveAs";
this.saveAs.Size = new System.Drawing.Size(94, 23);
this.saveAs.TabIndex = 5;
this.saveAs.Text = "Save As";
this.saveAs.UseVisualStyleBackColor = true;
this.saveAs.Click += new System.EventHandler(this.saveAs_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 23);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(39, 13);
this.label1.TabIndex = 6;
this.label1.Text = "Width:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(79, 23);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Height:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.saveAs);
this.Controls.Add(this.inputWidth);
this.Controls.Add(this.inputHeigh);
this.Controls.Add(this.save);
this.Controls.Add(this.showResult);
this.Controls.Add(this.open);
this.Name = "Form1";
this.Text = "Crazy7-ThumbnailGen";
((System.ComponentModel.ISupportInitialize)(this.showResult)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button open;
private System.Windows.Forms.OpenFileDialog openPicDialog;
private System.Windows.Forms.PictureBox showResult;
private System.Windows.Forms.Button save;
private System.Windows.Forms.TextBox inputHeigh;
private System.Windows.Forms.TextBox inputWidth;
private System.Windows.Forms.SaveFileDialog savePicDialog;
private System.Windows.Forms.Button saveAs;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace BmpOper
...{
public partial class Form1 : Form
...{
string picPath;
public Form1()
...{
InitializeComponent();
picPath = "";
}
private void open_Click(object sender, EventArgs e)
...{
//open source picture and save the path of it to "picPath":
if (openPicDialog.ShowDialog()== DialogResult.OK)
...{
picPath=openPicDialog.FileName;
4000
showResult.ImageLocation = picPath; //preview
}
}
private void save_Click(object sender, EventArgs e)
...{
//get Current directory and setup the name of Thumbnail:
string savePath = picPath.Substring(0, picPath.LastIndexOf(".")) + "Thum.jpg";//get the dir
//Transfer the Function :
GenerateThum(savePath);
}
private void GenerateThum(string savePath) //Generate the Thumbnail of open picture
...{
if (picPath != "")
...{
Validate the TextBox#region Validate the TextBox
string tempW = inputWidth.Text;
string tempH = inputHeigh.Text;
if (!Regex.IsMatch(tempW, @"^[0-9]+$") && Regex.IsMatch(tempH, @"^[0-9]+$"))
...{
MessageBox.Show("Error,pleae input a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
inputWidth.Focus();
}
#endregion
//get the width&height&savepath
int width = Int32.Parse(inputWidth.Text);
int height = Int32.Parse(inputHeigh.Text);
string ThumbPath = savePath;
//Generate
Bitmap newBmp = new Bitmap(picPath);
Image Thumbnail = newBmp.GetThumbnailImage(width,height, null, IntPtr.Zero);
//Save
Thumbnail.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private void saveAs_Click(object sender, EventArgs e) //save as
...{
if (savePicDialog.ShowDialog() == DialogResult.OK)
...{
string savePath = savePicDialog.FileName;
GenerateThum(savePath);
}
}
}
1871f
}
/**//*
* Crazy7
* @ 2007/04/08 pm
* A program for generate thumbnail of a picture.
*/
Form1.Designer.cs:
namespace BmpOper
...{
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);
}
Windows Form Designer generated code#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.open = new System.Windows.Forms.Button();
this.openPicDialog = new System.Windows.Forms.OpenFileDialog();
this.showResult = new System.Windows.Forms.PictureBox();
this.save = new System.Windows.Forms.Button();
this.inputHeigh = new System.Windows.Forms.TextBox();
this.inputWidth = new System.Windows.Forms.TextBox();
this.savePicDialog = new System.Windows.Forms.SaveFileDialog();
this.saveAs = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.showResult)).BeginInit();
this.SuspendLayout();
//
// open
//
this.open.Location = new System.Drawing.Point(192, 89);
this.open.Name = "open";
this.open.Size = new System.Drawing.Size(94, 24);
this.open.TabIndex = 0;
this.open.Text = "Select Picture";
this.open.UseVisualStyleBackColor = true;
this.open.Click += new System.EventHandler(this.open_Click);
//
// openPicDialog
//
this.openPicDialog.Filter = "jpg|*.jpg|bmp|*.bmp";
//
// showResult
//
this.showResult.Location = new System.Drawing.Point(12, 89);
this.showResult.Name = "showResult";
this.showResult.Size = new System.Drawing.Size(174, 161);
this.showResult.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.showResult.TabIndex = 1;
this.showResult.TabStop = false;
//
// save
//
this.save.Location = new System.Drawing.Point(192, 159);
this.save.Name = "save";
this.save.Size = new System.Drawing.Size(94, 24);
this.save.TabIndex = 2;
this.save.Text = "Save Picutre";
this.save.UseVisualStyleBackColor = true;
this.save.Click += new System.EventHandler(this.save_Click);
//
// inputHeigh
//
this.inputHeigh.Location = new System.Drawing.Point(91, 39);
this.inputHeigh.Name = "inputHeigh";
this.inputHeigh.Size = new System.Drawing.Size(41, 23);
this.inputHeigh.TabIndex = 3;
this.inputHeigh.Text = "75";
//
// inputWidth
//
this.inputWidth.Location = new System.Drawing.Point(22, 39);
this.inputWidth.Name = "inputWidth";
this.inputWidth.Size = new System.Drawing.Size(42, 23);
this.inputWidth.TabIndex = 4;
this.inputWidth.Text = "100";
//
// savePicDialog
//
this.savePicDialog.Filter = "jpg|*.jpg";
//
// saveAs
//
this.saveAs.Location = new System.Drawing.Point(192, 227);
this.saveAs.Name = "saveAs";
this.saveAs.Size = new System.Drawing.Size(94, 23);
this.saveAs.TabIndex = 5;
this.saveAs.Text = "Save As";
this.saveAs.UseVisualStyleBackColor = true;
this.saveAs.Click += new System.EventHandler(this.saveAs_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 23);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(39, 13);
this.label1.TabIndex = 6;
this.label1.Text = "Width:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(79, 23);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Height:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.saveAs);
this.Controls.Add(this.inputWidth);
this.Controls.Add(this.inputHeigh);
this.Controls.Add(this.save);
this.Controls.Add(this.showResult);
this.Controls.Add(this.open);
this.Name = "Form1";
this.Text = "Crazy7-ThumbnailGen";
((System.ComponentModel.ISupportInitialize)(this.showResult)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button open;
private System.Windows.Forms.OpenFileDialog openPicDialog;
private System.Windows.Forms.PictureBox showResult;
private System.Windows.Forms.Button save;
private System.Windows.Forms.TextBox inputHeigh;
private System.Windows.Forms.TextBox inputWidth;
private System.Windows.Forms.SaveFileDialog savePicDialog;
private System.Windows.Forms.Button saveAs;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}
相关文章推荐
- 在编写网站的时候,给一个div设置背景图片,怎么让这个图片随着div大小的改变,而等比例的缩放。
- 记几个常用的动态改变图片大小布局的东东
- 一个可以根据设定输出图片的函数 可以改变图片的大小
- 使用mspaint改变图片大小的一个小技巧记录
- Android 获取屏幕尺寸与密度 遇到一个问题,我的地图浮标图片在WVGA手机上正好,在QVGA上就显的太大,所以我要根据屏幕的不同调整浮标的大小使其在QVGA大小合适。有的同事提出了依据
- 图片改变大小加水印加文字
- 零基础ios开发(一 改变图片大小和位置)
- (12)改变图片的大小生成缩略图
- 根据产品写的一个可拖拽的,可改变大小的小型插件
- RadioButton动态改变drawableTop的图片大小
- vs2012偷梁换柱法之改变一个没有m_hWnd的active控件窗口大小
- 鼠标移入移出改变图片大小
- VC2010“添加资源->引入JPG图片”会改变图片大小
- 鼠标滚轮改变图片大小的示例代码
- 用struts实现单个文件上传并改变上传图片的大小
- iOS--UIGraphicsBeginImageContext系列知识(图片缩放裁剪、改变大小、合并、裁剪部分、保存到媒体库)
- photoshop批处理改变图片大小
- 使用CAShapeLayer实现一个音量大小动态改变的控件
- iOS一种简单的改变一个label上范围内字体颜色和大小的方法
- UIBarButtonItem 、UITabBarItem 改变字体,大小,图片