protected override void RunOverride(MainForm form, object tag) { TempFileCollection files = null; IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapEncoderInfo info = null; IWICBitmapEncoder encoder = null; IWICStream stream = null; int i = 0; try { files = new TempFileCollection(); info = (IWICBitmapEncoderInfo)factory.CreateComponentInfo(Parent.Clsid); do { stream.ReleaseComObject(); encoder.ReleaseComObject(); stream = factory.CreateStream(); stream.InitializeFromFilename(files.AddExtension(i.ToString(CultureInfo.InvariantCulture)), NativeMethods.GenericAccessRights.GENERIC_WRITE); i++; try { encoder = info.CreateInstance(); encoder.Initialize(stream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); break; } }while(ProcessEncoder(form, encoder, tag)); } finally { factory.ReleaseComObject(); info.ReleaseComObject(); encoder.ReleaseComObject(); stream.ReleaseComObject(); if (files != null) { files.Delete(); } } }
IEnumerable <string> GetDecodableFiles(MainForm form, Guid decoderClsid) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = null; IWICBitmapDecoder decoder = null; try { info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(decoderClsid); if (Settings.Default.Files != null) { foreach (string s in Settings.Default.Files) { IWICStream stream = factory.CreateStream(); try { stream.InitializeFromFilename(s, NativeMethods.GenericAccessRights.GENERIC_READ); bool matches = info.MatchesPattern(stream); stream.Seek(0, 0, IntPtr.Zero); decoder = info.CreateInstance(); if (decoder != null) { WICBitmapDecoderCapabilities?capabilities = null; try { capabilities = decoder.QueryCapability(stream); if (!matches) { QueryCapabilitiesError(form, string.Format(CultureInfo.CurrentUICulture, Resources.PatternDoesnotMatchButPassed, "IWICBitmapDecoder::QueryCapability(...)"), new DataEntry(s)); } } catch (Exception e) { if (Array.IndexOf(queryCapabilitiesErrors, (WinCodecError)Marshal.GetHRForException(e)) < 0) { QueryCapabilitiesError(form, e.TargetSite.ToString(Resources._0_FailedWithIncorrectHRESULT), new DataEntry(s), new DataEntry(Resources.Actual, e), new DataEntry(Resources.Expected, queryCapabilitiesErrors)); } } if (capabilities.HasValue && 0 != (uint)(capabilities.Value & (WICBitmapDecoderCapabilities.WICBitmapDecoderCapabilityCanDecodeSomeImages | WICBitmapDecoderCapabilities.WICBitmapDecoderCapabilityCanDecodeAllImages))) { yield return(s); } } } finally { stream.ReleaseComObject(); decoder.ReleaseComObject(); decoder = null; } } } } finally { factory.ReleaseComObject(); info.ReleaseComObject(); decoder.ReleaseComObject(); } }
protected override void RunOverride(MainForm form, object tag) { bool hasFile = false; bool processed = false; IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = null; IWICBitmapDecoder decoder = null; try { info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(Parent.Clsid); foreach (string file in GetDecodableFiles(form, Parent.Clsid)) { decoder.ReleaseComObject(); decoder = info.CreateInstance(); hasFile = true; IWICStream stream = factory.CreateStream(); stream.InitializeFromFilename(file, NativeMethods.GenericAccessRights.GENERIC_READ); try { decoder.Initialize(stream, WICDecodeOptions.WICDecodeMetadataCacheOnDemand); if (ProcessDecoder(form, decoder, new DataEntry[] { new DataEntry(file) }, tag)) { for (uint index = 0; index < decoder.GetFrameCount(); index++) { IWICBitmapFrameDecode frame = decoder.GetFrame(index); try { if (ProcessFrameDecode(form, frame, new DataEntry[] { new DataEntry(file), new DataEntry(index) }, tag)) { processed = true; } } finally { frame.ReleaseComObject(); } } } else { processed = true; } } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(file), new DataEntry(e)); } finally { stream.ReleaseComObject(); } } } finally { factory.ReleaseComObject(); info.ReleaseComObject(); decoder.ReleaseComObject(); } if (!hasFile) { form.Add(Parent, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Parent.Text)); } else if (!processed) { form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Text)); } }