.net2.0 生成缩略图,与指定比例不等自动补白边的方法
2007-07-05 00:21
519 查看
//int_Height int_Width 指定高度和指定宽度 input_Imgfile,out_ImgFile为原图片和缩小后图片的路径。
public static void Thumbnail (int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)
{
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);
float New_Width; // 新的宽度
float New_Height; // 新的高度
float Old_Width,Old_Height; //原始高宽
int flat = 0;//标记图片是不是等比
int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
int yPoint=0;
//判断图片
Old_Width = (float)oldimage.Width;
Old_Height = (float)oldimage.Height;
if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //当图片太宽的时候
{
New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
New_Width = (float)int_Width;
//此时x坐标不用修改
yPoint = (int)(((float)int_Height - New_Height) / 2);
flat = 1;
}
else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))
{
New_Width = int_Width;
New_Height = int_Height;
}
else
{
New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height); //太高的时候
New_Height = int_Height;
//此时y坐标不用修改
xPoint = (int)(((float)int_Width - New_Width) / 2);
flat = 1;
}
// ===缩小图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm = new Bitmap(thumbnailImage);
if (flat != 0)
{
Bitmap bmOutput = new Bitmap(int_Width,int_Height);
Graphics gc = Graphics.FromImage(bmOutput);
SolidBrush tbBg = new SolidBrush(Color.White);
gc.FillRectangle(tbBg,0, 0, int_Width, int_Height); //填充为白色
gc.DrawImage(bm,xPoint, yPoint, (int)New_Width, (int)New_Height);
bmOutput.Save(out_ImgFile);
}
else
{
bm.Save(out_ImgFile);
}
}
public static void Thumbnail (int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)
{
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);
float New_Width; // 新的宽度
float New_Height; // 新的高度
float Old_Width,Old_Height; //原始高宽
int flat = 0;//标记图片是不是等比
int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
int yPoint=0;
//判断图片
Old_Width = (float)oldimage.Width;
Old_Height = (float)oldimage.Height;
if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //当图片太宽的时候
{
New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
New_Width = (float)int_Width;
//此时x坐标不用修改
yPoint = (int)(((float)int_Height - New_Height) / 2);
flat = 1;
}
else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))
{
New_Width = int_Width;
New_Height = int_Height;
}
else
{
New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height); //太高的时候
New_Height = int_Height;
//此时y坐标不用修改
xPoint = (int)(((float)int_Width - New_Width) / 2);
flat = 1;
}
// ===缩小图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm = new Bitmap(thumbnailImage);
if (flat != 0)
{
Bitmap bmOutput = new Bitmap(int_Width,int_Height);
Graphics gc = Graphics.FromImage(bmOutput);
SolidBrush tbBg = new SolidBrush(Color.White);
gc.FillRectangle(tbBg,0, 0, int_Width, int_Height); //填充为白色
gc.DrawImage(bm,xPoint, yPoint, (int)New_Width, (int)New_Height);
bmOutput.Save(out_ImgFile);
}
else
{
bm.Save(out_ImgFile);
}
}
相关文章推荐
- 织梦dedecms将指定图片自动生成指定尺寸的小图、缩略图、图片的方法
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- DEDECMS 自动缩略图无法生成问题的解决方法
- PHP实现原比例生成缩略图的方法
- java读取数据库指定表,生成bean,获取主键,自动生成get、set方法
- php上传图片自动生成缩略图方法函数
- JUnit4---实践二:拓展之运行指定类的某些测试方法-自动生成该文件
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图的方法
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
- php实现根据url自动生成缩略图的方法
- php实现根据url自动生成缩略图的方法
- PHP实现原比例生成缩略图的方法
- java实现创建缩略图、伸缩图片比例生成的方法
- php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
- 用JavaScript自动生成等比例缩略图
- [蛙蛙推荐]用JS自动生成等比例缩略图