问题描述
大家好,
有人知道如何使用C#提取文件的缩略图吗?如果知道,请告诉我,谢谢.
Does anybody know how to extract the file'thumbnail use C# ?.If you know, please tell me,thank you.
推荐答案
在Windows资源管理器中看到的缩略图"存储在Windows生成的thumbs.db系统文件中,每当您以缩略图"查看模式查看文件夹时.如果您从不使用Windows资源管理器中的缩略图"视图,则该文件甚至不存在.
The 'thumbnails' you see in Windows Explorer are stored in the thumbs.db system file that is generated by Windows whenever you view a folder in 'Thumbnail' view mode. If you never use the Thumbnail view in Windows explorer, the file won't even exist.
基本上,有两种从图片中获取缩略图的方法.
Basically, there are 2 means of getting a thumbnail from a picture.
2.创建自己的缩略图.
2. Create your own thumbnail.
对于第一种方法,您需要一个EXIF解析器才能从JPEG读取EXIF标签.这实际上很复杂,但是这是一个带有库的示例,您可以下载http://www.vbaccelerator.com/home/NET/Code/Libraries/Graphics/Reading_EXIF_Tags_from_JPG_Files/article .asp
对于第二种方法,这是一种将创建指定大小&尺寸的图像的缩略图的方法.保持宽高比:
For the second method, here's a method that will create a thumbnail of an image in the specified size & keeps the aspect ratio:
int sourceHeight = img.Height;
int sourceHeight = img.Height;
int sourceX = 0;
int sourceX = 0;
int sourceY = 0;
int sourceY = 0;
int destX = 0;
int destX = 0;
int destY = 0;
int destY = 0;
float nPercent = 0;
float nPercent = 0;
浮动 nPercentW = 0;
float nPercentW = 0;
浮动 nPercentH = 0;
float nPercentH = 0;
nPercentW =((( float )Width/( float )sourceWidth);
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH =(( float )高度/( float )sourceHeight);
nPercentH = ((float)Height / (float)sourceHeight);
nPercent = nPercentH;
nPercent = nPercentH;
destX =系统. 转换 .ToInt16((Width-
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent))/2);
(sourceWidth * nPercent)) / 2);
int destWidth =( int )(sourceWidth * nPercent);
int destWidth = (int)(sourceWidth * nPercent);
int destHeight =( int )(sourceHeight * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
System.Drawing. 位图 bmPhoto = 新建 系统绘图. 位图 (宽度,高度,
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(Width, Height,
System.Drawing.Imaging. PixelFormat .Format24bppRgb);
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(img.HorizontalResolution,
bmPhoto.SetResolution(img.HorizontalResolution,
img.VerticalResolution);
img.VerticalResolution);
使用 (System.Drawing. 图形 grPhoto = System.Drawing. 图形 .FromImage(bmPhoto) ){
using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) {
grPhoto.InterpolationMode =
grPhoto.InterpolationMode =
System.Drawing.Drawing2D. InterpolationMode .HighQualityBicubic;
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(img,
grPhoto.DrawImage(img,
新 System.Drawing. 矩形 (destX,destY,destWidth,destHeight)
new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),
新 System.Drawing. 矩形 (sourceX,sourceY,sourceWidth,sourceHeight)
new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing. GraphicsUnit .Pixel);
System.Drawing.GraphicsUnit.Pixel);
}
返回 bmPhoto;
return bmPhoto;
}
注意:从文件流加载图像以获得最佳性能:
System.IO. FileInfo fi = new System.IO. FileInfo ( @" c:\ image.jpg" );
System.IO.FileInfo fi = new System.IO.FileInfo(@"c:\image.jpg");
使用 (System.IO. FileStream str = fi.OpenRead()){
using (System.IO.FileStream str = fi.OpenRead()) {
}
//创建100 * 100缩略图
图片缩略图 = GetImage (img,100,100);
Image thumb = GetImage(img, 100, 100);
希望这会有所帮助
这篇关于关于文件缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!