private void bgWDoWork_DoWork(object sender, DoWorkEventArgs e) { MyUserState myUserState = new MyUserState(); string[] allTiffs = Directory.GetFiles(tiffFile, "*.tiff", SearchOption.AllDirectories); int done = 0; foreach (string tiffFile2 in allTiffs) { using (Stream imageStreamSource = new FileStream(tiffFile2, FileMode.Open, FileAccess.Read, FileShare.Read)) { TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); myUserState.totalFrame = decoder.Frames.Count; for (int i = 0; i < decoder.Frames.Count; ++i) { myUserState.curFrame = i + 1; if (bgWDoWork.CancellationPending) { e.Cancel = true; myUserState.message = "User canceled"; break; } BitmapSource bigmapSource = decoder.Frames[i]; string fName = tiffFile2.Substring(tiffFile.Length); fName = fName.Substring(0, fName.LastIndexOf(".")); //Path.GetFileNameWithoutExtension(tiffFile) + (i > 0 ? (i + 1).ToString().PadLeft(padlength, '0') : ""); string jpegFileName = String.Format("{0}\\{1}.jpg", oulputFolder, fName); myUserState.message = String.Format("Converting {0} ...({1}/{2})", Path.GetFileNameWithoutExtension(jpegFileName), myUserState.curFrame, myUserState.totalFrame); bgWDoWork.ReportProgress(++done * 100 / allTiffs.Length, myUserState); Console.WriteLine(done * 100 / allTiffs.Length); Console.WriteLine("UpdateProgression : " + done * 100 / allTiffs.Length); Directory.CreateDirectory(Path.GetDirectoryName(jpegFileName)); using (FileStream stream = new FileStream(jpegFileName, FileMode.Create)) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = 100;// pCentCompression; encoder.Frames.Add(BitmapFrame.Create(bigmapSource)); encoder.Save(stream); } } //for (int i = 0; i < decoder.Frames.Count; ++i) } //using(Stream imageStreamSource = new FileStream(tiffFile, FileMode.Open, FileAccess.Read, FileShare.Read)) } //for each files myUserState.message = "Done converting Tiffs"; bgWDoWork.ReportProgress(100, myUserState); }
private void bgWDoWork_DoWork(object sender, DoWorkEventArgs e) { MyUserState myUserState = new MyUserState(); int padlength = 0; using (Stream imageStreamSource = new FileStream(tiffFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); myUserState.totalFrame = decoder.Frames.Count; if (hasPreZero) { padlength = (int)Math.Floor(Math.Log10((double)myUserState.totalFrame)); } for (int i = 0; i < decoder.Frames.Count; ++i) { myUserState.curFrame = i + 1; if (bgWDoWork.CancellationPending) { e.Cancel = true; myUserState.message = "User canceled"; bgWDoWork.ReportProgress(100, myUserState); break; } BitmapSource bigmapSource = decoder.Frames[i]; string jpegFileName = String.Format("{3}\\{0}{1}.{2}", filePre, (i + 1).ToString().PadLeft(padlength, '0'), fileExt, oulputFolder); myUserState.message = String.Format("Converting {0} ...({1}/{2})", Path.GetFileName(jpegFileName), myUserState.curFrame, myUserState.totalFrame); bgWDoWork.ReportProgress(100, myUserState); using (FileStream stream = new FileStream(jpegFileName, FileMode.Create)) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = 80; encoder.Frames.Add(BitmapFrame.Create(bigmapSource)); encoder.Save(stream); } } //for (int i = 0; i < decoder.Frames.Count; ++i) } //using(Stream imageStreamSource = new FileStream(tiffFile, FileMode.Open, FileAccess.Read, FileShare.Read)) }
private void BgWWatermarks_DoWork(object sender, DoWorkEventArgs e) { MyUserState myUserState = new MyUserState(); string[] allImages = Directory.GetFiles(tbSansWater.Text, "*.jpg", SearchOption.AllDirectories); int done = 0; foreach (string jpgFile in allImages) { using (Stream imageStreamSource = new FileStream(jpgFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (bgWWatermarks.CancellationPending) { e.Cancel = true; myUserState.message = "User canceled"; break; } string fName = jpgFile.Substring(tbSansWater.Text.Length); fName = fName.Substring(0, fName.LastIndexOf(".")); string jpegFileName = String.Format("{0}\\{1}.jpg", tbWithWater.Text, prefix + fName + suffix); myUserState.message = String.Format("Converting {0} ...({1}/{2})", Path.GetFileNameWithoutExtension(jpegFileName), myUserState.curFrame, myUserState.totalFrame); bgWWatermarks.ReportProgress(++done * 100 / allImages.Length, myUserState); Directory.CreateDirectory(Path.GetDirectoryName(jpegFileName)); if (pCentWaterSize <= 0) { pngWatermark = jpgFile; } using (Image imageOri = Image.FromFile(jpgFile)) using (Image image = convert("path", imageOri)) using (Image watermarkImage = Image.FromFile(pngWatermark)) // using (Graphics imageGraphics = convert("path",image)) using (Graphics imageGraphics = Graphics.FromImage(image)) using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage)) { if (pCentWaterSize > 0) { float ratioToPrint = pCentWaterSize * Math.Min(100 * image.Width / watermarkImage.Width, 100 * image.Height / watermarkImage.Height) / (100f * 100f); int x = (int)(image.Width / 2 - watermarkImage.Width * ratioToPrint / 2); int y = (int)(image.Height / 2 - watermarkImage.Height * ratioToPrint / 2); watermarkBrush.TranslateTransform(x - ratioToPrint * watermarkImage.Width, y - ratioToPrint * watermarkImage.Height); watermarkBrush.ScaleTransform(ratioToPrint, ratioToPrint); imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size((int)(ratioToPrint * watermarkImage.Width + 1), (int)(ratioToPrint * watermarkImage.Height)))); } ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)pCentCompression); myEncoderParameters.Param[0] = myEncoderParameter; image.Save(jpegFileName, myImageCodecInfo, myEncoderParameters); } } //using(Stream imageStreamSource = new FileStream(tiffFile, FileMode.Open, FileAccess.Read, FileShare.Read)) } //for each files myUserState.message = "Done adding Watermarks"; bgWWatermarks.ReportProgress(100, myUserState); }