public static int GetImageIndex(string filePath, string id, ImageList lstImg) { string fileExtention = Path.GetExtension(filePath); string keyword = fileExtention; try { if (!lstImg.Images.Keys.Contains(fileExtention)) { //图片文件,寻找图片缩略图 if (fileExtention.ToLower().EndsWith(".png") || fileExtention.ToLower().EndsWith(".bmp") || fileExtention.ToLower().EndsWith(".jpeg") || fileExtention.ToLower().EndsWith(".jpg")) { keyword = id; Image targetImg = null; string tempDir = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), "64"); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } string tempPath = Path.Combine(tempDir, id + ".jpeg"); Image sourceImg = Image.FromFile(filePath); targetImg = GetReducedImage(sourceImg, 64, 64); targetImg.Save(tempPath, ImageFormat.Jpeg); lstImg.Images.Add(keyword, targetImg); } else { keyword = fileExtention; lstImg.Images.Add(keyword, IconsExtention.IconFromExtension(fileExtention, IconsExtention.SystemIconSize.Large)); } } } catch (Exception) { if (fileExtention.EndsWith(".exe")) { keyword = fileExtention; lstImg.Images.Add(keyword, Resources.exe); } } return(lstImg.Images.IndexOfKey(keyword)); }
public static int GetImageIndex(TDataInfoDTO dto, ImageList lstImg) { string fileExtention = Path.GetExtension(dto.DataName); string keyword = fileExtention; try { if (!lstImg.Images.Keys.Contains(fileExtention)) { //图片文件,寻找图片缩略图 if (fileExtention.ToLower().EndsWith(".png") || fileExtention.ToLower().EndsWith(".bmp") || fileExtention.ToLower().EndsWith(".jpeg") || fileExtention.ToLower().EndsWith(".jpg")) { keyword = dto.MetaDataId; Image targetImg = null; string tempDir = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), "64"); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } string tempPath = Path.Combine(tempDir, dto.MetaDataId + ".jpeg"); if (!File.Exists(tempPath)) { //下载缩略图 string temp = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), dto.MetaDataId + fileExtention); long total = 0; byte[] buffer = null; bool success = true; while (success) { using (FileStream stream = new FileStream(temp, FileMode.Append)) { success = _client.TDataInfoDownloadFile(out buffer, total, dto.MetaDataId); if (success == false) { break; } total += buffer.Length; stream.Write(buffer, 0, buffer.Length); } } Image sourceImg = Image.FromFile(temp); int width = sourceImg.Width * 64 / sourceImg.Height; targetImg = GetReducedImage(sourceImg, width, 64); targetImg.Save(tempPath, ImageFormat.Jpeg); // File.Delete(temp); } else { targetImg = Image.FromFile(tempPath); } lstImg.Images.Add(keyword, targetImg); } else { keyword = fileExtention; lstImg.Images.Add(keyword, IconsExtention.IconFromExtension(fileExtention, IconsExtention.SystemIconSize.Large)); } } } catch (Exception) { if (fileExtention.EndsWith(".exe")) { keyword = fileExtention; lstImg.Images.Add(keyword, Resources.exe); } } return(lstImg.Images.IndexOfKey(keyword)); }