/// <summary> /// Resizes and saves the image as the asset ID jpg /// </summary> /// <param name="VAsset">VOD Asset</param> /// <param name="Height">Final height of the image</param> /// <param name="Width">Final width of the image</param> /// <param name="token">Cancellation token</param> private int ProcessImage(VODAsset VAsset, int Height, int Width, string vhoName, CancellationToken token) { token.ThrowIfCancellationRequested(); //Temporary folder path used to backup any existing images before any manipulation string tmpPath = Path.Combine(Directory.GetCurrentDirectory(), "Temp"); string tmpFile = string.Empty; //Create root temp folder if it doesn't exist if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } //Make a temp path child folder by VHO name to prevent asset id conflicts tmpPath = Path.Combine(tmpPath, vhoName); if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } try { //Poster destination file name cannot be null if (string.IsNullOrEmpty(VAsset.PosterDest)) { throw new ArgumentNullException("Destination folder cannot be null. " + VAsset.ToString()); } //Verify file extension is .jpg if (!VAsset.PosterDest.EndsWith(".jpg")) { throw new ArgumentException("(ProcessImages) Invalid destination file name.", VAsset.PosterDest); } //Throw error if source file not found, used to populate missing poster log if (!File.Exists(VAsset.PosterSource)) { throw new FileNotFoundException(string.Format("(ProcessImages) Source poster file not found. AssetID: {0}", VAsset.AssetId), VAsset.PosterSource); } //if destination file already exists, check if it needs updated using timestamp if (File.Exists(VAsset.PosterDest)) { tmpFile = Path.Combine(tmpPath, Path.GetFileName(VAsset.PosterDest)); //If a temp file exists in the temp directory for this asset, remove it if (File.Exists(tmpFile)) { File.Delete(tmpFile); } //Move the file to the temp directory, and set it as a temp file File.Move(VAsset.PosterDest, tmpFile); File.SetAttributes(tmpFile, FileAttributes.Temporary); } //Resize source image file if it is above 50MB try { FileInfo srcFInfo = new FileInfo(VAsset.PosterSource); if (((srcFInfo.Length / 1024F) / 1024F) > 50) { string tmpName = Path.Combine(Path.GetFullPath(srcFInfo.FullName), Path.GetFileNameWithoutExtension(srcFInfo.FullName) + "_tmp.jpg"); using (var origBM = new Bitmap(srcFInfo.FullName)) using (var srcBM = new Bitmap(origBM, Width, Height)) { srcBM.Save(tmpName, ImageFormat.Jpeg); } File.Copy(tmpName, srcFInfo.FullName, true); File.Delete(tmpName); } } catch (Exception ex) { Trace.TraceError("Failed to resize source image file above 50mb. {0}", ex.Message); } //Resize poster, save, and increment progress success if no errors using (var sourceBM = new Bitmap(VAsset.PosterSource)) using (var destBM = new Bitmap(sourceBM, Width, Height)) //using (var destBM = Toolset.ResizeBitmap(sourceBM, Width, Height, null, null, true)) { token.ThrowIfCancellationRequested(); destBM.Save(VAsset.PosterDest, ImageFormat.Jpeg); } //Verify file was saved and it exists if (!File.Exists(VAsset.PosterDest)) { throw new Exception("(ProcessImages) File failed to save in destination folder."); } return(0); } catch { //Restore backup file from temp directory if (!string.IsNullOrEmpty(tmpFile) && File.Exists(tmpFile)) { File.Copy(tmpFile, VAsset.PosterDest, true); } throw; } finally { //Delete backup file from temp directory if (!string.IsNullOrEmpty(tmpFile) && File.Exists(tmpFile)) { File.Delete(tmpFile); } } }