按照指定大小,生成缩略图

缩略图

/*
说明:按照指定大小,生成缩略图
作者:Blue.Dream
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//声明加密字符生成的随机数的位数
public const int LENGTH = 32;
//声明加密字符前后的分隔长度
public const int LEN = 4;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button btn_Browser;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.PictureBox imgSource;
private System.Windows.Forms.PictureBox imgObject;
private string SourceFile;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox local;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.Init();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

private void Init()
{
DataTable ht = new DataTable();
DataColumn objcol = new DataColumn("Keys");
objcol.DataType = typeof(System.String);
ht.Columns.Add(objcol);
objcol = new DataColumn("Value");
objcol.DataType = typeof(System.Int32);
ht.Columns.Add(objcol);
DataRow row = ht.NewRow();
row[0] = "800*600";
row[1] = 1;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "640*480";
row[1] = 2;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "420*360";
row[1] = 3;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "340*270";
row[1] = 4;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "240*180";
row[1] = 5;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "120*90";
row[1] = 6;
ht.Rows.Add(row);

this.local.DataSource = ht;
this.local.DisplayMember = "Keys";
this.local.ValueMember = "Value";
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.imgSource = new System.Windows.Forms.PictureBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.imgObject = new System.Windows.Forms.PictureBox();
this.btn_Browser = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.local = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.imgSource});
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(288, 296);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "源图";
//
// imgSource
//
this.imgSource.Location = new System.Drawing.Point(8, 16);
this.imgSource.Name = "imgSource";
this.imgSource.Size = new System.Drawing.Size(272, 272);
this.imgSource.TabIndex = 0;
this.imgSource.TabStop = false;
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.imgObject});
this.groupBox2.Location = new System.Drawing.Point(304, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(352, 296);
this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "结果图";
//
// imgObject
//
this.imgObject.Location = new System.Drawing.Point(8, 16);
this.imgObject.Name = "imgObject";
this.imgObject.Size = new System.Drawing.Size(336, 272);
this.imgObject.TabIndex = 0;
this.imgObject.TabStop = false;
//
// btn_Browser
//
this.btn_Browser.Location = new System.Drawing.Point(24, 312);
this.btn_Browser.Name = "btn_Browser";
this.btn_Browser.TabIndex = 1;
this.btn_Browser.Text = "源 图";
this.btn_Browser.Click += new System.EventHandler(this.btn_Browser_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(552, 312);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "生 成";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// local
//
this.local.Location = new System.Drawing.Point(256, 312);
this.local.Name = "local";
this.local.Size = new System.Drawing.Size(112, 20);
this.local.TabIndex = 4;
this.local.Text = "--请选择--";
//
// label1
//
this.label1.Location = new System.Drawing.Point(216, 312);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 6;
this.label1.Text = "大小";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 365);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.local,
this.button2,
this.btn_Browser,
this.groupBox1,
this.groupBox2});
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btn_Browser_Click(object sender, System.EventArgs e)
{
using(OpenFileDialog dlg = new OpenFileDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
string file = dlg.FileName;
if(File.Exists(file))
{
this.SourceFile = file;
imgSource.Image = new Bitmap(file);
}
}
dlg.Dispose();
}
}

private void button2_Click(object sender, System.EventArgs e)
{
int select = (int)this.local.SelectedValue;
Point p = new Point(340,270);
switch(select)
{
case 1:
p.X = 800;
p.Y = 600;
break;
case 2:
p.X = 640;
p.Y = 480;
break;
case 3:
p.X = 480;
p.Y = 360;
break;
case 4:
p.Y = 340;
p.X = 270;
break;
case 5:
p.X = 240;
p.Y = 180;
break;
case 6:
p.X = 120;
p.Y = 90;
break;
}

//生成目标文件地址
FileInfo objFile = new FileInfo(this.SourceFile);
string filepath = objFile.DirectoryName;
string filename = objFile.Name;
string saveFile = filepath + "\\Thumb"+filename;
if(MakeThumbnailImage(this.SourceFile,saveFile,p.X,p.Y,objFile.Extension.ToLower()))
this.imgObject.Image = new Bitmap(saveFile);
else
MessageBox.Show("FAILD");

}

/// <summary>
/// 按照指定大小,生成缩略图
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="saveFile">存储文件</param>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图的高度</param>
/// <param name="strType">文件类型</param>
/// <returns></returns>
private bool MakeThumbnailImage(string sourceFile,string saveFile,int width,int height,string strType)
{
if( !File.Exists(sourceFile) ) //判断文件是否存在
return false;
bool result = true;
Bitmap sourceImage = new Bitmap(sourceFile);
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallBack);
Image objImage = sourceImage.GetThumbnailImage(width,height,myCallBack,IntPtr.Zero); //取得缩略图

try
{
objImage.Save(saveFile,GetImageType(strType));
}
catch
{
result = false;
}
finally
{
objImage.Dispose();
sourceImage.Dispose();
}
return result;

}

private System.Drawing.Imaging.ImageFormat GetImageType(string strType)
{
switch(strType)
{
case ".jpg":
return ImageFormat.Jpeg;
case ".jpeg":
return ImageFormat.Jpeg;
case ".gif":
return ImageFormat.Gif;
case ".bmp":
return ImageFormat.Bmp;
case ".tiff":
return ImageFormat.Tiff;
case ".ico":
return ImageFormat.Icon;
case ".png":
return ImageFormat.Png;
case ".emf":
return ImageFormat.Emf;
default:
return ImageFormat.Jpeg;
}
}

private bool ThumbnailCallBack()
{
return false;
}

}
}

时间: 2024-09-20 00:55:36

按照指定大小,生成缩略图的相关文章

用AspJpeg组件,按宽高比例,真正生成缩略图

aspjpeg|缩略图 在网站前台产品展示时,一般用缩略图,点击进入然后看到大图.缩略图带来了两个烦劳:1.如果后台只传一张大图,显示缩略图时只是将大图固定宽度和高度,这样不但造成缩略图变形,而且使得页面访问速度缓慢.2.如果后台每次上传时,都上传两张图片,一张大图,一张缩略图.这样的话,没有1中的问题,但是给后台人员造成很大麻烦.因为后台人员并不一定知道处理生成缩略图:即使知道并能快速处理,也浪费掉一些时间. 下面的代码可以帮您用AspJpeg组件,按宽高比例,真正生成缩略图AspJpeg组件

asp图片上传后用aspjpeg自动生成缩略图

 Aspjpeg 功能设置 Set Jpeg = Server.CreateObject("Persits.Jpeg") ' 获取源图片路径 Path = Server.MapPath("uploadimages/")&"/"&FileName ' 打开源图片 Jpeg.Open Path ' 设定生成缩略图细节 这里有很多种设定方法 下面的方法是先判断宽高比 然后按比例缩放 If Jpeg.OriginalWidth / Jpe

php实现按指定大小等比缩放生成上传图片缩略图的方法_php技巧

本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @par

php按指定大小等比缩放生成上传图片缩略图

php教程按指定大小等比缩放生成上传图片缩略图 /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @param unknown_type $imgQuality 图片质量  * @retur

C# 生成指定大小缩略图

/// <summary> /// 生成缩略图 /// </summary> /// <param name= "originalImagePath ">源图路径(物理路径) </param> /// <param name= "thumbnailPath "> 缩略图路径(物理路径) </param> /// <param name= "width "> 缩略图

按照比例改变图片大小(非生成缩略图)

  <? /**      按照比例改变图片大小(非生成缩略图)      @param string $img 图片路径      @param int $max_w 最大缩放宽      @param int $max_h 最大缩放高  */  function chImageSize ($img,$max_w,$max_h)  {      $size = @getimagesize($img);          $w = $size[0];          $h     =    $

PHP动态生成指定大小随机图片的方法_php技巧

本文实例讲述了PHP动态生成指定大小随机图片的方法.分享给大家供大家参考,具体如下: <?php $image_width = 100; $image_height = 100; $image_str = ''; if (isset($_GET['w'])) { $image_width = intval($_GET['w']); } if (isset($_GET['h'])) { $image_height = intval($_GET['h']); } if (isset($_GET['s

求一js代码 用于在Gridview中显示缩略图 图片较大 要缩小真实大小生成新的小图片

问题描述 求一js代码用于在Gridview中显示缩略图图片较大要缩小真实大小生成新的小图片 解决方案 解决方案二:js????js除了能显示服务器上的图片?还能修改服务器上的图片了??小图片都是在大图片上传上去后就生成的,如何生成小图去看System.Drawing.Image解决方案三:引用1楼starfd的回复: js????js除了能显示服务器上的图片?还能修改服务器上的图片了??小图片都是在大图片上传上去后就生成的,如何生成小图去看System.Drawing.Image up解决方案

[转]c# 添加图片水印,可以指定水印位置+生成缩略图

 c# 添加图片水印,可以指定水印位置+生成缩略图       早上,一哥儿发来添加图片水印的资料.有三个信息,如下:              xx 09:57:35 http://index.cnblogs.com/archive/2004/10/20/54498.aspx  王二狗 09:57:51 好的,我看看     xx 09:58:12 http://www.iyuanma.com/info/18/17026_200592663244.htm  xx 10:07:00 http:/