/// <summary> /// 生成缩略图 /// </summary> /// <param name="source">原图片</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static Image CreateThumbnail(this Image source, int width, int height, ThumbnailMode mode) { return(Images.CreateThumbnail(source, width, height, mode)); }
public async Task <Stream> GetThumbnailAsync(CancellationToken ct, ThumbnailMode mode, int size) { return(await GetThumbnailAsync(ct, mode)); }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) => throw new NotSupportedException();
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="thumbnailMode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode thumbnailMode) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (thumbnailMode) { case ThumbnailMode.None: if (ow > oh) { toheight = originalImage.Height * width / originalImage.Width; } else if (oh > ow) { towidth = originalImage.Width * height / originalImage.Height; } break; case ThumbnailMode.HW: //指定高宽缩放(可能变形) break; case ThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { thumbnailPath = thumbnailPath.Replace("/", "\\"); string savePath = thumbnailPath.Substring(0, thumbnailPath.LastIndexOf("\\")); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } bitmap.Save(thumbnailPath, GetImageFormat(thumbnailPath)); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
public IAsyncOperation <StorageItemThumbnail> GetScaledImageAsThumbnailAsync(ThumbnailMode mode) { return(Task.FromResult <StorageItemThumbnail>(null).AsAsyncOperation()); }
/// <summary> /// 创建高清缩略图 /// </summary> /// <param name="sourceImage">原图路径</param> /// <param name="targetImage">缩略图保存路径</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="mode">尺寸模式</param> /// <remarks> /// 2013-03-14 罗雄伟 创建 /// </remarks> public static void CreateThumbnail(string sourceImage, string targetImage, int width, int height, ThumbnailMode mode) { using (Image source = Image.FromFile(sourceImage)) { switch (mode) { case ThumbnailMode.Width: height = (int)Math.Round(source.Height * ((double)width / source.Width)); break; case ThumbnailMode.Height: width = (int)Math.Round(source.Width * ((double)height / source.Height)); break; case ThumbnailMode.WidthHeighLimitted: int w = (int)Math.Round(source.Width * ((double)height / width)); int h = (int)Math.Round(source.Height * ((double)width / height)); break; } //Image target = source.GetThumbnailImage(width, height, () => { return false; }, IntPtr.Zero); //target.Save(targetImage, ImageFormat.Jpeg); using (Bitmap target = new Bitmap(width, height)) { Graphics g = Graphics.FromImage(target); g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(0, 0, source.Width, source.Height), GraphicsUnit.Pixel); EncoderParameters parameters = new EncoderParameters(); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { 100 }); string lookupKey = "image/jpeg"; var codecJpg = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType.Equals(lookupKey)).FirstOrDefault(); targetImage = Path.Combine(Path.GetDirectoryName(targetImage), Path.GetFileNameWithoutExtension(targetImage) + ".jpg"); if (!Directory.Exists(Path.GetDirectoryName(targetImage))) { Directory.CreateDirectory(Path.GetDirectoryName(targetImage)); } target.Save(targetImage, codecJpg, parameters); } } }
public static async Task <byte[]> LoadIconFromStorageItemAsync(IStorageItem item, uint thumbnailSize, ThumbnailMode thumbnailMode) { if (item.IsOfType(StorageItemTypes.File)) { using var thumbnail = (StorageItemThumbnail) await FilesystemTasks.Wrap( () => item.AsBaseStorageFile ().GetThumbnailAsync (thumbnailMode, thumbnailSize, ThumbnailOptions.ResizeThumbnail).AsTask()); if (thumbnail != null) { return(await thumbnail.ToByteArrayAsync()); } } else if (item.IsOfType(StorageItemTypes.Folder)) { using var thumbnail = (StorageItemThumbnail) await FilesystemTasks.Wrap( () => item.AsBaseStorageFolder ().GetThumbnailAsync (thumbnailMode, thumbnailSize, ThumbnailOptions.ResizeThumbnail).AsTask()); if (thumbnail != null) { return(await thumbnail.ToByteArrayAsync()); } } return(null); }
/// <summary> /// Retrieves an adjusted thumbnail image for the file, determined by the /// purpose of the thumbnail, the requested size, and the specified /// options. /// </summary> /// <param name="mode">The enum value that describes the purpose of the thumbnail and determines how the thumbnail image is adjusted.</param> /// <param name="requestedSize">The requested size, in pixels, of the longest edge of the thumbnail. Windows uses the requestedSize as a guide and tries to scale the thumbnail image without reducing the quality of the image.</param> /// <param name="options">The enum value that describes the desired behavior to use to retrieve the thumbnail image. The specified behavior might affect the size and/or quality of the image and how quickly the thumbnail image is retrieved.</param> /// <returns>When this method completes successfully, it returns a StorageItemThumbnail that represents the thumbnail image or null if there is no thumbnail image associated with the file.</returns> public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) { throw new NotImplementedException(); }
public static void MakeThumbnail(string originalPath, string thumbnailPath, int width, int height, ThumbnailMode mode) { var originalImage = Image.FromFile(originalPath); var towidth = width; var toheight = height; var ow = originalImage.Width; var oh = originalImage.Height; var x = 0; var y = 0; switch (mode) { case ThumbnailMode.HeightWidth: break; case ThumbnailMode.Width: if (originalImage.Width > width) toheight = originalImage.Height*width/originalImage.Width; else { towidth = originalImage.Width; toheight = originalImage.Height; } break; case ThumbnailMode.Height: if (originalImage.Height > height) towidth = originalImage.Width*height/originalImage.Height; else { towidth = originalImage.Width; toheight = originalImage.Height; } break; case ThumbnailMode.Cut: if (originalImage.Width/(double) originalImage.Height > towidth/(double) toheight) { oh = originalImage.Height; ow = originalImage.Height*towidth/toheight; y = 0; x = (originalImage.Width - ow)/2; } else { ow = originalImage.Width; oh = originalImage.Width*height/towidth; x = 0; y = (originalImage.Height - oh)/2; } break; } var bitmap = new Bitmap(towidth, toheight); var graphics = Graphics.FromImage(bitmap); graphics.InterpolationMode = InterpolationMode.High; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.Clear(Color.Transparent); graphics.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { bitmap.Save(thumbnailPath, ImageFormat.Jpeg); } catch (Exception ex) { LocalLoggingService.Exception(ex); } finally { originalImage.Dispose(); bitmap.Dispose(); graphics.Dispose(); } }
public static async Task <byte[]> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode) { if (!filePath.EndsWith(".lnk", StringComparison.Ordinal) && !filePath.EndsWith(".url", StringComparison.Ordinal)) { var item = await StorageHelpers.ToStorageItem <IStorageItem>(filePath); if (item != null) { var iconData = await LoadIconFromStorageItemAsync(item, thumbnailSize, thumbnailMode); if (iconData != null) { return(iconData); } } } return(await LoadIconWithoutOverlayAsync(filePath, thumbnailSize)); }
/// <summary> /// 上传图片并生成缩略图 /// </summary> /// <param name="fileUpload">上传FileUpload控件</param> /// <param name="saveFolderPath">保存文件的目录</param> /// <param name="fileTypes">允许上传文件类型 多个请用英文逗号隔开</param> /// <param name="allowedMaxSize">上传文件大小上限 单位:KB</param> /// <param name="allowedWidth">允许图片宽度</param> /// <param name="allowedHeight">允许图片高度</param> /// <param name="thumbnailWidth">缩略图宽度</param> /// <param name="thumbnailHeight">缩略图高度</param> /// <param name="thumbnailMode">缩略类型</param> /// <param name="errors">错误信息</param> /// <returns></returns> public static string UploadImageAndMakeThumbnail(HtmlInputFile fileUpload, string saveFolderPath, string fileTypes, int allowedMaxSize, int allowedWidth, int allowedHeight, int thumbnailWidth, int thumbnailHeight, ThumbnailMode thumbnailMode, out string errors) { if (null == fileUpload || fileUpload.PostedFile.ContentLength <= 0) { errors = "没有选定被上传的文件!"; return string.Empty; } //上传图片 string uploadedFileName = UploadImage(fileUpload, saveFolderPath, fileTypes, allowedMaxSize, allowedWidth, allowedHeight, out errors); if (string.IsNullOrEmpty(uploadedFileName)) { return string.Empty; } if (!saveFolderPath.EndsWith("/")) { saveFolderPath = VirtualPathUtility.AppendTrailingSlash(saveFolderPath); } uploadedFileName = uploadedFileName.Replace(saveFolderPath, ""); string fullUploadedImagePath = mUrl.Combine(saveFolderPath, uploadedFileName); string fullThumbnailPath = mUrl.Combine(saveFolderPath, "t" + uploadedFileName); //生成缩略图 mImage ih = new mImage(thumbnailWidth, thumbnailHeight, (int)WaterType.None, (int)thumbnailMode); string thumbnailPath = ih.MakeThumbnail(fullUploadedImagePath, fullThumbnailPath, thumbnailWidth, thumbnailHeight); //string thumbnailPath = // Client.Web.mImage.GenThumbnailImage(fullUploadedImagePath, fullThumbnailPath, thumbnailWidth, thumbnailHeight, thumbnailMode); return thumbnailPath.Replace(saveFolderPath, ""); }
/// <summary> /// 创建高清缩略图 /// </summary> /// <param name="imgStream">原图路径</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="mode">尺寸模式</param> /// <returns>图片流</returns> public static MemoryStream CreateThumbnail(Stream imgStream, int width, int height, ThumbnailMode mode) { MemoryStream ms = new MemoryStream(); using (Image source = Image.FromStream(imgStream)) { #region 计算坐标和宽高 int x = 0; int y = 0; int ow = source.Width; int oh = source.Height; switch (mode) { case ThumbnailMode.Width: height = (int)Math.Round(source.Height * ((double)width / source.Width)); break; case ThumbnailMode.Height: width = (int)Math.Round(source.Width * ((double)height / source.Height)); break; case ThumbnailMode.Cut: //指定高宽裁减(不变形) yhy if ((double)source.Width / (double)source.Height > (double)width / (double)height) { oh = source.Height; ow = source.Height * width / height; y = 0; x = (source.Width - ow) / 2; } else { ow = source.Width; oh = source.Width * height / width; x = 0; y = (source.Height - oh) / 2; } break; } #endregion #region 尺寸不变则直接返回 if (width == ow && height == oh) { imgStream.Position = 0; imgStream.CopyTo(ms); return(ms); } #endregion #region 生成缩略图 using (Bitmap bmp = new Bitmap(width, height)) using (Graphics g = Graphics.FromImage(bmp)) { g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); EncoderParameters parameters = new EncoderParameters(); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { 100 }); string lookupkey = "image/jpeg"; var codecjpg = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType.Equals(lookupkey)).FirstOrDefault(); bmp.Save(ms, codecjpg, parameters); } #endregion } return(ms); }
/// <summary> /// 生成缩率图 /// </summary> /// <param name="originalImage">原始图片Image</param> /// <param name="width">缩率图宽</param> /// <param name="height">缩率图高</param> /// <param name="mode">生成缩率图的方式</param> /// <param name="thumbnailPath">缩率图存放的地址</param> public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailMode mode, string thumbnailPath, bool isSave = true) { int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ThumbnailMode.HW: //指定高宽缩放(可能变形) break; case ThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); if (!isSave) { return(bitmap); } try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, ImageFormat.Jpeg); return(bitmap); } catch (Exception) { return(null); } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) { return(File.GetThumbnailAsync(mode, requestedSize, options)); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="sourceImage">源图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图方式</param> public static Image MakeThumbnail(Image sourceImage, int width, int height, ThumbnailMode mode) { var towidth = width; var toheight = height; var x = 0; var y = 0; var ow = sourceImage.Width; var oh = sourceImage.Height; switch (mode) { case ThumbnailMode.FixedBoth: break; case ThumbnailMode.FixedW: toheight = oh * width / ow; break; case ThumbnailMode.FixedH: towidth = ow * height / oh; break; case ThumbnailMode.Cut: if (ow / (double)oh > towidth / (double)toheight) { oh = sourceImage.Height; ow = sourceImage.Height * towidth / toheight; y = 0; x = (sourceImage.Width - ow) / 2; } else { ow = sourceImage.Width; oh = sourceImage.Width * height / towidth; x = 0; y = (sourceImage.Height - oh) / 2; } break; } //1、新建一个BMP图片 var bitmap = new Bitmap(towidth, toheight); //2、新建一个画板 var g = Graphics.FromImage(bitmap); try { //3、设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //4、设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //5、清空画布并以透明背景色填充 g.Clear(Color.Transparent); //6、在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(sourceImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); return(bitmap); } catch (Exception e) { throw e; } finally { g.Dispose(); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="isDate">是否按日期创建目录</param> /// <param name="isCreateNewFileName">是否创建新文件名</param> /// <param name="isImage">是否是图片</param> /// <param name="isThumbnail">是否生成缩图</param> /// <param name="thumbnailWidth">缩图宽度</param> /// <param name="thumbnailHeight">缩图高度</param> /// <param name="thumbnailMode">缩图模式</param> /// <param name="isWatermarkText">是否加水印文字</param> /// <param name="watermarkText">水印文字</param> public FileUpLoadHelper(String filePath, Boolean isDate, Boolean isCreateNewFileName, Boolean isImage, Boolean isThumbnail, Int32 thumbnailWidth, Int32 thumbnailHeight, ThumbnailMode thumbnailMode, Boolean isWatermarkText, String watermarkText) { _isThumbnail = isThumbnail; _filePath = filePath; _isDate = isDate; _isImage = isImage; _isCreateNewFileName = isCreateNewFileName; _thumbnailWidth = thumbnailWidth; _thumbnailHeight = thumbnailHeight; _thumbnailMode = thumbnailMode; _isWatermarkText = isWatermarkText; _watermarkText = watermarkText; if (_isDate) _filePath += DateTime.Now.ToString("yyyyMMdd") + "/"; String savePath = HttpContext.Current.Server.MapPath(_filePath); if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath); }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize) { return(Folder.GetThumbnailAsync(mode, requestedSize)); }
public static void MakeThumbnailImage(string inImgPath, string outImgPath, int thumWidth, int thumHeight, ThumbnailMode mode) { Image srcImage = Image.FromFile(inImgPath); GetThumbnailImage(srcImage, new SizeF(thumWidth, thumHeight), mode).Save(outImgPath); srcImage.Dispose(); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ThumbnailMode.HW: //指定高宽缩放(可能变形) break; case ThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 //第一个:对哪张图片进行操作。 //二:画多么大。 //三:画那块区域。 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 // bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Save(thumbnailPath, originalImage.RawFormat); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
public IAsyncOperation <StorageItemThumbnail> GetScaledImageAsThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) { return(Task.FromResult <StorageItemThumbnail>(null).AsAsyncOperation()); }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize) { return(Task.FromResult <StorageItemThumbnail>(null).AsAsyncOperation()); }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode) => throw new NotSupportedException();
public ViewMode(ThumbnailMode type, string icon, Visibility visibility = Visibility.Collapsed) { Type = type; Icon = icon; Visibility = visibility; }
public async Task <IDownloadResponse <FileMetadata> > GetThumbnailAsync(string path, ThumbnailFormat format = null, ThumbnailSize size = null, ThumbnailMode mode = null) => await _dropBoxClient.Files.GetThumbnailAsync(path, format, size, mode);
public async void MemoryFriendlyGetItemsAsync(string path, CancellationToken token) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); PUIP.Path = path; try { PVIS.isVisible = Visibility.Visible; TextState.isVisible = Visibility.Collapsed; folder = await StorageFolder.GetFolderFromPathAsync(path); QueryOptions options = new QueryOptions() { FolderDepth = FolderDepth.Shallow, IndexerOption = IndexerOption.UseIndexerWhenAvailable }; if (sort == "By_Name") { entry = new SortEntry() { AscendingOrder = true, PropertyName = "System.FileName" }; } options.SortOrder.Add(entry); uint index = 0; const uint step = 250; if (!folder.AreQueryOptionsSupported(options)) { options.SortOrder.Clear(); } folderQueryResult = folder.CreateFolderQueryWithOptions(options); IReadOnlyList <StorageFolder> folders = await folderQueryResult.GetFoldersAsync(index, step); int foldersCountSnapshot = folders.Count; while (folders.Count != 0) { foreach (StorageFolder folder in folders) { if (token.IsCancellationRequested) { return; } gotFolName = folder.Name.ToString(); gotFolDate = folder.DateCreated.ToString(); gotFolPath = folder.Path.ToString(); gotFolType = "Folder"; gotFolImg = Visibility.Visible; gotFileImgVis = Visibility.Collapsed; gotEmptyImgVis = Visibility.Collapsed; if (pageName == "ClassicModePage") { ClassicFolderList.Add(new Classic_ListedFolderItem() { FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath }); } else { FilesAndFolders.Add(new ListedItem() { EmptyImgVis = gotEmptyImgVis, ItemIndex = FilesAndFolders.Count, FileImg = null, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotFolName, FileDate = gotFolDate, FileExtension = gotFolType, FilePath = gotFolPath }); } } index += step; folders = await folderQueryResult.GetFoldersAsync(index, step); } index = 0; fileQueryResult = folder.CreateFileQueryWithOptions(options); IReadOnlyList <StorageFile> files = await fileQueryResult.GetFilesAsync(index, step); int filesCountSnapshot = files.Count; while (files.Count != 0) { foreach (StorageFile file in files) { if (token.IsCancellationRequested) { return; } gotName = file.DisplayName.ToString(); gotDate = file.DateCreated.ToString(); // In the future, parse date to human readable format if (file.FileType.ToString() == ".exe") { gotType = "Executable"; } else { gotType = file.DisplayType; } gotPath = file.Path.ToString(); gotFolImg = Visibility.Collapsed; gotDotFileExtension = file.FileType; if (isPhotoAlbumMode == false) { const uint requestedSize = 20; const ThumbnailMode thumbnailMode = ThumbnailMode.ListView; const ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale; try { gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions); BitmapImage icon = new BitmapImage(); if (gotFileImg != null) { gotEmptyImgVis = Visibility.Collapsed; icon.SetSource(gotFileImg.CloneStream()); } else { gotEmptyImgVis = Visibility.Visible; } gotFileImgVis = Visibility.Visible; if (pageName == "ClassicModePage") { ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); } else { FilesAndFolders.Add(new ListedItem() { DotFileExtension = gotDotFileExtension, EmptyImgVis = gotEmptyImgVis, FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); } } catch { // Silent catch here to avoid crash // TODO maybe some logging could be added in the future... } } else { const uint requestedSize = 275; const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView; const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail; try { gotFileImg = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions); BitmapImage icon = new BitmapImage(); if (gotFileImg != null) { gotEmptyImgVis = Visibility.Collapsed; icon.SetSource(gotFileImg.CloneStream()); } else { gotEmptyImgVis = Visibility.Visible; } gotFileImgVis = Visibility.Visible; if (pageName == "ClassicModePage") { ClassicFileList.Add(new ListedItem() { FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); } else { FilesAndFolders.Add(new ListedItem() { EmptyImgVis = gotEmptyImgVis, FileImg = icon, FileIconVis = gotFileImgVis, FolderImg = gotFolImg, FileName = gotName, FileDate = gotDate, FileExtension = gotType, FilePath = gotPath }); } } catch { // Silent catch here to avoid crash // TODO maybe some logging could be added in the future... } } } index += step; files = await fileQueryResult.GetFilesAsync(index, step); } if (foldersCountSnapshot + filesCountSnapshot == 0) { TextState.isVisible = Visibility.Visible; } if (pageName != "ClassicModePage") { PVIS.isVisible = Visibility.Collapsed; } PVIS.isVisible = Visibility.Collapsed; stopwatch.Stop(); Debug.WriteLine("Loading of: " + path + " completed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); } catch (UnauthorizedAccessException e) { if (path.Contains(@"C:\")) { DisplayConsentDialog(); } else { MessageDialog unsupportedDevice = new MessageDialog("This device may be unsupported. Please file an issue report in Settings - About containing what device we couldn't access. Technical information: " + e, "Unsupported Device"); await unsupportedDevice.ShowAsync(); } stopwatch.Stop(); Debug.WriteLine("Loading of: " + path + " failed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); } catch (COMException e) { stopwatch.Stop(); Debug.WriteLine("Loading of: " + path + " failed in " + stopwatch.ElapsedMilliseconds + " Milliseconds."); Frame rootFrame = Window.Current.Content as Frame; MessageDialog driveGone = new MessageDialog(e.Message, "Drive Unplugged"); await driveGone.ShowAsync(); rootFrame.Navigate(typeof(MainPage), new SuppressNavigationTransitionInfo()); } }
/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="height">高度</param> /// <param name="width"></param> /// <param name="flag">压缩质量 1-100</param> /// <param name="type">压缩缩放类型</param> /// <returns></returns> public static bool MakeThumbnail(string sFile, string dFile, int height, int width, ThumbnailMode type, int flag = 100) { try { KalikoImage image = new KalikoImage(sFile); image.BackgroundColor = Color.Aquamarine; //缩放后的宽度和高度 int towidth = width; int toheight = height; // int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; switch (type) { case ThumbnailMode.HW: //指定高宽缩放(可能变形) { break; } case ThumbnailMode.W: //指定宽,高按比例 { toheight = image.Height * width / image.Width; break; } case ThumbnailMode.H: //指定高,宽按比例 { towidth = image.Width * height / image.Height; break; } case ThumbnailMode.Cut: //指定高宽裁减(不变形) { if ((double)image.Width / (double)image.Height > (double)towidth / (double)toheight) { oh = image.Height; ow = image.Height * towidth / toheight; y = 0; x = (image.Width - ow) / 2; } else { ow = image.Width; oh = image.Width * height / towidth; x = 0; y = (image.Height - oh) / 2; } break; } default: break; } var img = image.Scale(new FitScaling(towidth, toheight)); var extension = System.IO.Path.GetExtension(sFile); switch (extension.ToLower()) { case ".png": img.SavePng(dFile); break; case ".gif": img.SaveGif(dFile); break; case ".ico": img.SaveImage(dFile, ImageFormat.Icon); break; case ".bmp": img.SaveBmp(dFile); break; case ".jpg": img.SaveJpg(dFile, flag); break; default: img.SaveBmp(dFile); break; } return(true); } catch { return(false); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="isDate">是否按日期创建目录</param> /// <param name="isCreateNewFileName">是否创建新文件名</param> /// <param name="isImage">是否是图片</param> /// <param name="isThumbnail">是否生成缩图</param> /// <param name="thumbnailWidth">缩图宽度</param> /// <param name="thumbnailHeight">缩图高度</param> /// <param name="thumbnailMode">缩图模式</param> /// <param name="isWatermarkText">是否加水印文字</param> /// <param name="watermarkText">水印文字</param> public FileUpLoadHelper(string filePath, bool isDate, bool isCreateNewFileName, bool isImage, bool isThumbnail, int thumbnailWidth, int thumbnailHeight, ThumbnailMode thumbnailMode, bool isWatermarkText, string watermarkText) { this._isThumbnail = isThumbnail; this._filePath = filePath; this._isDate = isDate; this._isImage = isImage; this._isCreateNewFileName = isCreateNewFileName; this._thumbnailWidth = thumbnailWidth; this._thumbnailHeight = thumbnailHeight; this._thumbnailMode = thumbnailMode; this._isWatermarkText = isWatermarkText; this._watermarkText = watermarkText; if (this._isDate) { this._filePath += DateTime.Now.ToString("yyyyMMdd") + "/"; } string savePath = HttpContext.Current.Server.MapPath(this._filePath); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } }
public static void GenerateThumbnail(string sourceFlie, string destinationFile, int width, int height, ThumbnailMode mode) { using (Image sourceImage = Image.FromFile(sourceFlie)) { switch (mode) { case ThumbnailMode.WH: break; case ThumbnailMode.W: height = sourceImage.Height * width / sourceImage.Width; break; case ThumbnailMode.H: width = sourceImage.Width * height / sourceImage.Height; break; } if (width > sourceImage.Width) { width = sourceImage.Width; } if (height > sourceImage.Height) { height = sourceImage.Height; } using (Bitmap bmp = new Bitmap(width, height)) //新建一个图片 { using (Graphics g = Graphics.FromImage(bmp)) //画板 { g.InterpolationMode = InterpolationMode.High; g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(sourceImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel); } bmp.Save(destinationFile); } } }
public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailMode mode) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int initWidth = originalImage.Width; int initHeight = originalImage.Height; switch (mode) { case ThumbnailMode.UsrHeightWidth: //指定高宽缩放(可能变形) break; case ThumbnailMode.UsrHeightWidthBound: //指定高宽缩放(可能变形)(过小则不变) if (originalImage.Width <= width && originalImage.Height <= height) { return(originalImage); } if (originalImage.Width < width) { toWidth = originalImage.Width; } if (originalImage.Height < height) { toHeight = originalImage.Height; } break; case ThumbnailMode.UsrWidth: //指定宽,高按比例 toHeight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.UsrWidthBound: //指定宽(过小则不变),高按比例 if (originalImage.Width <= width) { return(originalImage); } else { toHeight = originalImage.Height * width / originalImage.Width; } break; case ThumbnailMode.UsrHeight: //指定高,宽按比例 toWidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.UsrHeightBound: //指定高(过小则不变),宽按比例 if (originalImage.Height <= height) { return(originalImage); } else { toWidth = originalImage.Width * height / originalImage.Height; } break; case ThumbnailMode.Cut: //指定高宽裁减(不变形) //计算宽高比 double srcScale = (double)originalImage.Width / (double)originalImage.Height; double destScale = (double)toWidth / (double)toHeight; //宽高比相同 if (srcScale - destScale >= 0 && srcScale - destScale <= 0.001) { x = 0; y = 0; initWidth = originalImage.Width; initHeight = originalImage.Height; } //源宽高比大于目标宽高比 //(源的宽比目标的宽大) else if (srcScale > destScale) { initWidth = originalImage.Height * toWidth / toHeight; initHeight = originalImage.Height; x = (originalImage.Width - initWidth) / 2; y = 0; } //源宽高比小于目标宽高小,源的高度大于目标的高度 else { initWidth = originalImage.Width; initHeight = originalImage.Width * height / toWidth; x = 0; y = (originalImage.Height - initHeight) / 2; } break; default: break; } Image bitmap = new Bitmap(toWidth, toHeight); //新建一个画板 using (Graphics g = Graphics.FromImage(bitmap)) { //设置高质量插值法 g.CompositingQuality = CompositingQuality.HighQuality; //设置高质量,低速段呈现的平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //在指定的位置上,并按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, initWidth, initHeight), GraphicsUnit.Pixel); } return(bitmap); }
public override IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode) { return(File.GetThumbnailAsync(mode)); }
private static async Task SaveThumbnail(StorageFile file, StorageFolder folder, ThumbnailMode mode) { using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(mode, 100)) { if (thumbnail != null && thumbnail.Type == ThumbnailType.Image) { var destinationFile = await folder.CreateFileAsync(ThumbnailName, CreationCollisionOption.GenerateUniqueName); using (var strm = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) { var buffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(thumbnail.Size)); IBuffer iBuf = await thumbnail.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.None); await strm.WriteAsync(iBuf); } } } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ThumbnailMode.HW://指定高宽缩放(可能变形) break; case ThumbnailMode.W://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.H://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.CUT://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath); //bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
public abstract IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode);
/// <summary> /// Retrieves an adjusted thumbnail image for the file, determined by the /// purpose of the thumbnail. /// </summary> /// <param name="mode">The enum value that describes the purpose of the thumbnail and determines how the thumbnail image is adjusted.</param> /// <returns>When this method completes successfully, it returns a StorageItemThumbnail that represents the thumbnail image or null if there is no thumbnail image associated with the file.</returns> public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode) { throw new NotImplementedException(); }
public abstract IAsyncOperation <StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options);
/// <summary> /// ��������ͼ /// </summary> /// <param name="originalImagePath">ԭͼƬ·��</param> /// <param name="thumbnailPath">����ͼ·��</param> /// <param name="width">���</param> /// <param name="height">�߶�</param> /// <param name="mode">����ģʽ</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ThumbnailMode.HW://ָ���߿����ţ����ܱ��Σ� break; case ThumbnailMode.W://ָ������߰����� toheight = originalImage.Height * width / originalImage.Width; break; case ThumbnailMode.H://ָ���ߣ�������� towidth = originalImage.Width * height / originalImage.Height; break; case ThumbnailMode.Cut://ָ���߿�ü��������Σ� if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //�½�һ��bmpͼƬ System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //�½�һ������ System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //���ø�������ֵ�� g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //���ø�����,���ٶȳ���ƽ���̶� g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //��ջ�������������ɫ��� g.Clear(System.Drawing.Color.Transparent); //��ָ��λ�ò��Ұ�ָ����С����ԭͼƬ��ָ������ g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //��jpg��ʽ��������ͼ bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
private static void SaveThumbnailPic(string sUploadPathAndFileName, string sSavePathAndFileName, int nWidth, int nHeight, ThumbnailMode mode) { sUploadPathAndFileName = HttpContext.Current.Server.MapPath(sUploadPathAndFileName); sSavePathAndFileName = HttpContext.Current.Server.MapPath(sSavePathAndFileName); CreateDirectory(sSavePathAndFileName); Image image = Image.FromFile(sUploadPathAndFileName); int x = 0, y = 0, iW = image.Width, iH = image.Height; int towidth = nWidth; int toheight = nHeight; Rectangle drawZone = new Rectangle(0, 0, towidth, toheight); Rectangle ImgZone = new Rectangle(0, 0, iW, iH); switch (mode) { case ThumbnailMode.HW: //缩小到指定大小 break; case ThumbnailMode.W: if (nWidth > image.Width) { nWidth = image.Width; nHeight = image.Height; drawZone = new Rectangle(0, 0, image.Width, image.Height); } else { nHeight = toheight = image.Height * nWidth / image.Width; drawZone = new Rectangle(0, 0, towidth, toheight); } break; case ThumbnailMode.H: if (nHeight > image.Height) { nWidth = image.Width; nHeight = image.Height; drawZone = new Rectangle(0, 0, image.Width, image.Height); } else { nWidth = towidth = image.Width * nHeight / image.Height; drawZone = new Rectangle(0, 0, towidth, toheight); } break; case ThumbnailMode.Cut: if ((double)iW / (double)iH > (double)nWidth / (double)nHeight) { iW = iH * nWidth / nHeight; x = (image.Width - iW) / 2; } else { iH = iW * nHeight / nWidth; y = (image.Height - iH) / 2; } ImgZone = new Rectangle(x, y, iW, iH); break; case ThumbnailMode.Ration: //拉伸 同HW break; case ThumbnailMode.Fill: if ((double)iW / (double)iH > (double)nWidth / (double)nHeight) { toheight = iH * nWidth / iW; y = (nHeight - toheight) / 2; } else { towidth = iW * nHeight / iH; x = (nWidth - towidth) / 2; } drawZone = new Rectangle(x, y, towidth, toheight); break; default: break; } Bitmap bitmap = new Bitmap(nWidth, nHeight); Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; ////设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //g.CompositingQuality = CompositingQuality.AssumeLinear; g.Clear(Color.White); g.DrawImage(image, drawZone, ImgZone, GraphicsUnit.Pixel); ImageFormat format = image.RawFormat; image.Dispose(); bitmap.Save(sSavePathAndFileName);//,ImageFormat.Png bitmap.Dispose(); g.Dispose(); }