Windows >
Software Development for Windows Client Forums
>
Windows Imaging Component (WIC)
>
The handle is invalid - Exception from HRESULT: 0x80070006 (E_HANDLE)
The handle is invalid - Exception from HRESULT: 0x80070006 (E_HANDLE)
- I'm trying to create a new (and smaller) JPEG file - based on a source JPEG - and then copy the metadata from the original JPEG to the new JPEG - and I'm getting the following exception at random intervals.
The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)).
Here's the code I'm using to create the new JPEG and conditionally place a copy of the metadata from the old JPEG into the new one if requested.
NOTE: The exception is ONLY thrown (and even then on totally random occasions) when the option to IncludeMetadata is true. About one in 50 or one in 100 times will throw this exception. Adding the frame to the encoder without metadata is fine.
var source = new Uri(SourceFile);
//We load a frame first so that we can 'peak' and get the image dimensions - this should be fast.
var frame = BitmapFrame.Create(source,
BitmapCreateOptions.IgnoreColorProfile |
BitmapCreateOptions.IgnoreImageCache |
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
var bmp = new BitmapImage();
bmp.CacheOption = BitmapCacheOption.Default;
bmp.BeginInit();
if (frame.PixelWidth <= frame.PixelHeight)
{
bmp.DecodePixelWidth = MaxDimension;
}
else
{
bmp.DecodePixelHeight = MaxDimension;
}
bmp.UriSource = source;
bmp.EndInit();
using (FileStream writer = new FileStream(TargetFile, FileMode.Create))
{
var encoder = new JpegBitmapEncoder();
encoder.QualityLevel = Quality;
if (IncludeMetadata)
{
BitmapMetadata bitmapMetadata = frame.Metadata.Clone() as BitmapMetadata;
encoder.Frames.Add(BitmapFrame.Create(bmp, null, bitmapMetadata, frame.ColorContexts));
}
else
{
encoder.Frames.Add(BitmapFrame.Create(bmp, null, null, frame.ColorContexts));
}
encoder.Save(writer);
writer.Flush();
}
Thoughts?
All Replies
- After searching and discovering this post...
http://social.msdn.microsoft.com/Forums/de-DE/wpf/thread/73fc7de6-904f-4b31-ac66-35ffb8a49122
As well as recalling that BitmapMetadata class has MTA issues - I set the thread that this code is running on to be
Thread.SetApartmentState(ApartmentState.STA);
Thread.IsBackground = false;
The application that uses the code above initializes a worker thread in the background for batch processing of images (not on the UI) - and so I'm guessing I was seeing the error above as a result?
Can anyone on the WIC team confirm that the BitmapMetadata class must still be created and used on an STA thread? (Which also suggests the use of COM/Interop underneath the BitmapMetadata class correct?) - > Can anyone on the WIC team confirm that the BitmapMetadata class must still be created and used on an STA thread? (Which also suggests the use of COM/Interop underneath the BitmapMetadata class correct?)
Why do you need the WIC team to confirm this?
WIC is a COM based technology, period. For any managed code to access it there has to be interop.
You may have better luck asking about WPF classes in their forum. The WPF stuff is built on top of WIC and isn't a direct mapping of the WIC interfaces. - Well I thought that since they wrote the component that BitmapMetadata calls/wraps - they would know which apartment model the component had been created for.
Posting to the WPF forum is a good idea - and so will do that as well.


