基本信息
源码名称:C#生成缩略图 实例源码
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2013-01-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
最简单的生成缩略图原理,.net版本
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
public bool ThumbnailCallback()
{
return false;
}
public bool GetReducedImage(double Percent, string targetFilePath)
{
try
{
Bitmap bt = new Bitmap(120, 120);
Graphics g = Graphics.FromImage(bt);
g.Clear(Color.White);
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
if (ImageWidth > ImageHeight)
{
g.DrawImage(ReducedImage, 0, (int)(120 - ImageHeight) / 2, ImageWidth, ImageHeight);
}
else
{
g.DrawImage(ReducedImage, (int)(120 - ImageWidth) / 2, 0, ImageWidth, ImageHeight);
}
g.DrawRectangle(new Pen(Color.Gainsboro), 0, 0, 119, 119);
bt.Save(@targetFilePath, ImageFormat.Jpeg);
bt.Dispose();
ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
double percent;
string imgpath = openFileDialog1.FileName;
string imgName = imgpath.ToString().Substring(imgpath.ToString().LastIndexOf("\\") 1, imgpath.ToString().Length - 1 - imgpath.ToString().LastIndexOf("\\"));
imgName = imgName.Remove(imgName.LastIndexOf("."));
if (openFileDialog1.FileName.Length != 0)
{
ResourceImage = Image.FromFile(openFileDialog1.FileName);
if (ResourceImage.Width < ResourceImage.Height)
{
percent = (double)120 / ResourceImage.Height;
}
else
{
percent = (double)120 / ResourceImage.Width;
}
GetReducedImage(percent, "c:\\_" imgName ".JPG");
pictureBox2.Image = Image.FromFile("c:\\_" imgName ".JPG");
}
}
}