//返回指定图片的缩略图(给定图片的路径) public static Image GetThumbnail(string imagePath, int longestSide) { Image thumbnail; Bitmap image; try { //读取图片 image = new Bitmap(imagePath); thumbnail = PhotoHelper.GetThumbnail(image, longestSide); } catch { return(null); } finally { //if (image != null) //{ // image.Dispose(); //} } return(thumbnail); }
/// <summary> /// 创建一个缩略图 /// </summary> /// <param name="photoPath">图片的路径信息</param> /// <param name="thumbnailFolder">缩略图所在文件夹的完整路径</param> /// <returns></returns> public static bool CreateThumbnail(string photoPath, string thumbnailFolder) { bool createdThumbnail = false; Image thumbnail; try { // get full path to the thumbnail image string thumbnailPath = Path.Combine(thumbnailFolder, GetThumbnailName(photoPath)); // see if the thumbnail already exist if (!File.Exists(thumbnailPath)) { // create the thumbnail image thumbnail = PhotoHelper.GetThumbnail(photoPath, ThumbnailSize.Large); // save thumbnail to file system thumbnail.Save(thumbnailPath, ImageFormat.Jpeg); createdThumbnail = true; // clean up if (!(thumbnail == null)) { thumbnail.Dispose(); } } } catch { // an error occurred, return null } finally { } return(createdThumbnail); }
// 此方法执行对图片的操作 private void ApplyType(ref Bitmap image) { if ((_curType == ActionType.None)) { return; } Bitmap newImage = null; try { switch (_curType) { case ActionType.Rotate: RotateFlipType rotateType = GetRotateFlipType(); if ((rotateType != RotateFlipType.RotateNoneFlipNone)) { newImage = new Bitmap(image); PhotoHelper.Rotate(newImage, rotateType); image.Dispose(); image = new Bitmap(newImage); } break; case ActionType.Crop: newImage = PhotoHelper.Crop(image, _crop); image.Dispose(); image = new Bitmap(newImage); break; case ActionType.FlipHorizontal: if (_flipHorz) { PhotoHelper.Rotate(image, RotateFlipType.RotateNoneFlipX); } break; case ActionType.FlipVertical: if (_flipVert) { PhotoHelper.Rotate(image, RotateFlipType.RotateNoneFlipY); } break; } } catch { // should not get here, catch error incase file // is deleted while applying actions } finally { // 清除操作 if (!(newImage == null)) { newImage.Dispose(); } // 将成员变量重新初始化为默认值 Clear(); } }
//返回表示深褐色度的矩阵 public static float[][] GetSepiaMatrix() { //如果先把图片变黑,深褐色会看上去更好一些,矩阵值(亮度和深褐色化的合并) //可以被直接编码,在代码中保留它们为的是更易于修改 float[][] bright = GetBrightnessMatrix(-25); //建立矩阵 float[][] sepia = { new float[] { Consts.GrayRed, Consts.GrayRed, Consts.GrayRed, 0, 0 }, new float[] { Consts.GrayGreen, Consts.GrayGreen, Consts.GrayGreen, 0, 0 }, new float[] { Consts.GrayBlue, Consts.GrayBlue, Consts.GrayBlue, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { Consts.SepiaRed, Consts.SepiaGreen, Consts.SepiaBlue, 0, 1 } }; return(PhotoHelper.CombineMatrix(bright, sepia)); }
//// public methods //// return a list of Photo objects that are initialized from //// metadata xml files if they exist //public static Photo[] GetPhotos(string path, string thumbnailFolder) //{ // string[] files = FileManager.GetPhotoFileList(path); // if ((files == null)) // { // return null; // } // Photo[] list = new Photo[files.Length]; // string thumbnailPath; // // go through and create a new instance of each Photo // for (int i = 0; (i // <= (files.Length - 1)); i++) // { // thumbnailPath = Path.Combine(Path.Combine(Path.GetDirectoryName(files[i]), thumbnailFolder), GetThumbnailName(files[i])); // // ReadXml returns a photo object. The Title, Description, // // and DateTaken are set if there is an xml file. Otherwise // // an empty object is returned. // list[i] = Photo.ReadXml(files[i]); // list[i].PhotoName = Path.GetFileNameWithoutExtension(files[i]); // list[i].PhotoPath = files[i]; // list[i].ThumbnailCode = GetThumbnailCode(files[i]); // list[i].ThumbnailPath = thumbnailPath; // } // return list; //} //获取照片对象列表 public static Photo[] GetPhotos(int FileID) { Photo[] list = null; DataSet ds = DataAccess.getPhotoData(FileID); if (ds != null && ds.Tables[0].Rows.Count > 0) { list = new Photo[ds.Tables[0].Rows.Count]; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Photo photo = new Photo(); photo.PhotoID = Convert.ToInt32(ds.Tables[0].Rows[i]["照片ID"]); photo.DocNumber = Convert.ToInt32(ds.Tables[0].Rows[i]["张号"]); photo.Title = ds.Tables[0].Rows[i]["题名"].ToString(); photo.PhotoNumber = ds.Tables[0].Rows[i]["照片号"].ToString(); photo.CJNumber = ds.Tables[0].Rows[i]["参见号"].ToString(); photo.Maker = ds.Tables[0].Rows[i]["摄影者"].ToString(); photo.Description = ds.Tables[0].Rows[i]["文字说明"].ToString(); photo.PhotoName = ds.Tables[0].Rows[i]["Ftp保存名称"].ToString(); photo.PhotoPath = ds.Tables[0].Rows[i]["Ftp目录"].ToString(); photo.DateTaken = getDateTime(ds.Tables[0].Rows[i]["时间"].ToString()); object image = ds.Tables[0].Rows[i]["缩略图"]; if ((Object.Equals(image, null)) || (Object.Equals(image, System.DBNull.Value)) || (Object.Equals(image, string.Empty))) { Image img = PhotoControl.Properties.Resources.no_photo_s; photo.Thumbnail = PhotoHelper.GetThumbnail(img, ThumbnailSize.Large); } else { MemoryStream ms = new MemoryStream((byte[])image); photo.Thumbnail = Image.FromStream(ms); } list[i] = photo; } } return(list); }
// 该方法对图片应用所有的颜色操作,以提供一个更加自然的用户体验。 // 原始的操作顺序被忽略,方法中按照如下顺序对图片进行颜色操作: // 1) 灰度转化和褐色转化 // 2) 对比度、亮度和饱和度调节 // 3) gamma参数调节 private void AdjustColor(ref Bitmap image) { int brightness = 0; int contrast = 0; int saturation = 0; int gamma = 0; ConvertColor convert = ConvertColor.None; // 遍历操作列表找到每一个颜色操作的最后设定值 for (int i = 0; i <= (Global.ActionList.Count - 1); i++) { ActionItem item = Global.ActionList.GetAt(i); switch (item.Action) { case PhotoAction.Brightness: brightness = item.Percent; break; case PhotoAction.Contrast: contrast = item.Percent; break; case PhotoAction.Saturation: saturation = item.Percent; break; case PhotoAction.Gamma: gamma = item.Percent; break; case PhotoAction.ConvertGrayscale: convert = ConvertColor.Grayscale; break; case PhotoAction.ConvertSepia: convert = ConvertColor.Sepia; break; } } // 执行颜色操作时更新进度条 Global.Progress.Update(this, "Applying actions", 1, Global.ActionList.Count); // 只有当列表中还有一个或多个操作要执行时才更新 if (((convert != ConvertColor.None) || ((contrast != 0) || ((brightness != 0) || (saturation != 0))))) { // 矩阵默认值(单位矩阵) float[][] matrix = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; // 灰度转化和褐色转化 if ((convert == ConvertColor.Grayscale)) { matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetGrayscaleMatrix()); } if ((convert == ConvertColor.Sepia)) { matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetSepiaMatrix()); } // 对比度、亮度和饱和度调节 if ((contrast != 0)) { matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetContrastMatrix(contrast)); } if ((brightness != 0)) { matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetBrightnessMatrix(brightness)); } if ((saturation != 0)) { matrix = PhotoHelper.CombineMatrix(matrix, PhotoHelper.GetSaturationMatrix(saturation)); } // 对图片执行所有颜色操作 PhotoHelper.AdjustUsingCustomMatrix(ref image, matrix); } // gamma 参数调节,作为最后一个操作来执行 if ((gamma != 0)) { PhotoHelper.AdjustGamma(ref image, gamma); } }