Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / System / Windows / Media / Imaging / JpegBitmapEncoder.cs / 1 / JpegBitmapEncoder.cs
//------------------------------------------------------------------------------
// Microsoft Avalon
// Copyright (c) Microsoft Corporation, All Rights Reserved
//
// File: JpegBitmapEncoder.cs
//
//-----------------------------------------------------------------------------
using System;
using System.Security;
using System.Security.Permissions;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using MS.Internal;
using MS.Win32.PresentationCore;
using System.Diagnostics;
using System.Windows.Media;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Media.Imaging
{
#region JpegBitmapEncoder
///
/// Built-in Encoder for Jpeg files.
///
public sealed class JpegBitmapEncoder : BitmapEncoder
{
#region Constructors
///
/// Constructor for JpegBitmapEncoder
///
///
/// Critical - will eventually create unmanaged resources
/// PublicOK - all inputs are verified
///
[SecurityCritical ]
public JpegBitmapEncoder() :
base(true)
{
_supportsPreview = false;
_supportsGlobalThumbnail = false;
_supportsGlobalMetadata = false;
_supportsFrameThumbnails = true;
_supportsMultipleFrames = false;
_supportsFrameMetadata = true;
}
#endregion
#region Public Properties
///
/// Set the quality level for the encoding.
/// The quality level must be between 1-100, inclusive.
/// This property is mutually exclusive with doing lossless encoding.
///
public int QualityLevel
{
get
{
return _qualityLevel;
}
set
{
if ((value < 1) || (value > 100))
{
throw new System.ArgumentOutOfRangeException("value", SR.Get(SRID.ParameterMustBeBetween, 1, 100));
}
_qualityLevel = value;
}
}
///
/// Set a lossless rotation value of Rotation degrees.
/// This replaces any previous lossless transformation.
///
public Rotation Rotation
{
get
{
if (Rotate90)
{
return Rotation.Rotate90;
}
else if (Rotate180)
{
return Rotation.Rotate180;
}
else if (Rotate270)
{
return Rotation.Rotate270;
}
else
{
return Rotation.Rotate0;
}
}
set
{
Rotate90 = false;
Rotate180 = false;
Rotate270 = false;
switch(value)
{
case(Rotation.Rotate0):
// do nothing, we reset everything above
// case statement is here for clearness
break;
case(Rotation.Rotate90):
Rotate90 = true;
break;
case(Rotation.Rotate180):
Rotate180 = true;
break;
case(Rotation.Rotate270):
Rotate270 = true;
break;
}
}
}
///
/// Set a lossless horizontal flip.
/// This replaces any previous lossless transformation.
///
public bool FlipHorizontal
{
get
{
return (Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal));
}
set
{
if (value != this.FlipHorizontal)
{
if (value)
{
_transformation |= WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal;
}
else
{
_transformation &= ~WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal;
}
}
}
}
///
/// Set a lossless vertical flip.
/// This replaces any previous lossless transformation.
///
public bool FlipVertical
{
get
{
return (Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformFlipVertical));
}
set
{
if (value != this.FlipVertical)
{
if (value)
{
_transformation |= WICBitmapTransformOptions.WICBitmapTransformFlipVertical;
}
else
{
_transformation &= ~WICBitmapTransformOptions.WICBitmapTransformFlipVertical;
}
}
}
}
#endregion
#region Internal Properties / Methods
///
/// Returns the container format for this encoder
///
///
/// Critical - uses guid to create unmanaged resources
///
internal override Guid ContainerFormat
{
[SecurityCritical]
get
{
return _containerFormat;
}
}
///
/// Setups the encoder and other properties before encoding each frame
///
///
/// Critical - calls Critical Initialize()
///
[SecurityCritical]
internal override void SetupFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions)
{
PROPBAG2 propBag = new PROPBAG2();
PROPVARIANT propValue = new PROPVARIANT();
// There are only two encoder options supported here:
if (_transformation != c_defaultTransformation)
{
try
{
propBag.Init("BitmapTransform");
propValue.Init((byte) _transformation);
HRESULT.Check(UnsafeNativeMethods.IPropertyBag2.Write(
encoderOptions,
1,
ref propBag,
ref propValue));
}
finally
{
propBag.Clear();
propValue.Clear();
}
}
if (_qualityLevel != c_defaultQualityLevel)
{
try
{
propBag.Init("ImageQuality");
propValue.Init( ((float)_qualityLevel) / 100.0f);
HRESULT.Check(UnsafeNativeMethods.IPropertyBag2.Write(
encoderOptions,
1,
ref propBag,
ref propValue));
}
finally
{
propBag.Clear();
propValue.Clear();
}
}
HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.Initialize(
frameEncodeHandle,
encoderOptions
));
}
///
/// Set a lossless rotation value of 90 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate90
{
get
{
return ((Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate90) && (!Rotate270)));
}
set
{
if (value != this.Rotate90)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate90;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
///
/// Set a lossless rotation value of 180 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate180
{
get
{
return ((Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate180) && (!Rotate270)));
}
set
{
if (value != this.Rotate180)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate180;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
///
/// Set a lossless rotation value of 270 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate270
{
get
{
return (Convert.ToBoolean(((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate270) == (int)WICBitmapTransformOptions.WICBitmapTransformRotate270));
}
set
{
if (value != this.Rotate270)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate270;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
#endregion
#region Internal Abstract
/// Need to implement this to derive from the "sealed" object
internal override void SealObject()
{
throw new NotImplementedException();
}
#endregion
#region Data Members
///
/// Critical - CLSID used for creation of critical resources
///
[SecurityCritical]
private Guid _containerFormat = MILGuidData.GUID_ContainerFormatJpeg;
// This happens to be the default used by the jpeg lib.
private const int c_defaultQualityLevel = 75;
private int _qualityLevel = c_defaultQualityLevel;
private const WICBitmapTransformOptions c_defaultTransformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
private WICBitmapTransformOptions _transformation = c_defaultTransformation;
#endregion
}
#endregion // JpegBitmapEncoder
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
// Microsoft Avalon
// Copyright (c) Microsoft Corporation, All Rights Reserved
//
// File: JpegBitmapEncoder.cs
//
//-----------------------------------------------------------------------------
using System;
using System.Security;
using System.Security.Permissions;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using MS.Internal;
using MS.Win32.PresentationCore;
using System.Diagnostics;
using System.Windows.Media;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
namespace System.Windows.Media.Imaging
{
#region JpegBitmapEncoder
///
/// Built-in Encoder for Jpeg files.
///
public sealed class JpegBitmapEncoder : BitmapEncoder
{
#region Constructors
///
/// Constructor for JpegBitmapEncoder
///
///
/// Critical - will eventually create unmanaged resources
/// PublicOK - all inputs are verified
///
[SecurityCritical ]
public JpegBitmapEncoder() :
base(true)
{
_supportsPreview = false;
_supportsGlobalThumbnail = false;
_supportsGlobalMetadata = false;
_supportsFrameThumbnails = true;
_supportsMultipleFrames = false;
_supportsFrameMetadata = true;
}
#endregion
#region Public Properties
///
/// Set the quality level for the encoding.
/// The quality level must be between 1-100, inclusive.
/// This property is mutually exclusive with doing lossless encoding.
///
public int QualityLevel
{
get
{
return _qualityLevel;
}
set
{
if ((value < 1) || (value > 100))
{
throw new System.ArgumentOutOfRangeException("value", SR.Get(SRID.ParameterMustBeBetween, 1, 100));
}
_qualityLevel = value;
}
}
///
/// Set a lossless rotation value of Rotation degrees.
/// This replaces any previous lossless transformation.
///
public Rotation Rotation
{
get
{
if (Rotate90)
{
return Rotation.Rotate90;
}
else if (Rotate180)
{
return Rotation.Rotate180;
}
else if (Rotate270)
{
return Rotation.Rotate270;
}
else
{
return Rotation.Rotate0;
}
}
set
{
Rotate90 = false;
Rotate180 = false;
Rotate270 = false;
switch(value)
{
case(Rotation.Rotate0):
// do nothing, we reset everything above
// case statement is here for clearness
break;
case(Rotation.Rotate90):
Rotate90 = true;
break;
case(Rotation.Rotate180):
Rotate180 = true;
break;
case(Rotation.Rotate270):
Rotate270 = true;
break;
}
}
}
///
/// Set a lossless horizontal flip.
/// This replaces any previous lossless transformation.
///
public bool FlipHorizontal
{
get
{
return (Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal));
}
set
{
if (value != this.FlipHorizontal)
{
if (value)
{
_transformation |= WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal;
}
else
{
_transformation &= ~WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal;
}
}
}
}
///
/// Set a lossless vertical flip.
/// This replaces any previous lossless transformation.
///
public bool FlipVertical
{
get
{
return (Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformFlipVertical));
}
set
{
if (value != this.FlipVertical)
{
if (value)
{
_transformation |= WICBitmapTransformOptions.WICBitmapTransformFlipVertical;
}
else
{
_transformation &= ~WICBitmapTransformOptions.WICBitmapTransformFlipVertical;
}
}
}
}
#endregion
#region Internal Properties / Methods
///
/// Returns the container format for this encoder
///
///
/// Critical - uses guid to create unmanaged resources
///
internal override Guid ContainerFormat
{
[SecurityCritical]
get
{
return _containerFormat;
}
}
///
/// Setups the encoder and other properties before encoding each frame
///
///
/// Critical - calls Critical Initialize()
///
[SecurityCritical]
internal override void SetupFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions)
{
PROPBAG2 propBag = new PROPBAG2();
PROPVARIANT propValue = new PROPVARIANT();
// There are only two encoder options supported here:
if (_transformation != c_defaultTransformation)
{
try
{
propBag.Init("BitmapTransform");
propValue.Init((byte) _transformation);
HRESULT.Check(UnsafeNativeMethods.IPropertyBag2.Write(
encoderOptions,
1,
ref propBag,
ref propValue));
}
finally
{
propBag.Clear();
propValue.Clear();
}
}
if (_qualityLevel != c_defaultQualityLevel)
{
try
{
propBag.Init("ImageQuality");
propValue.Init( ((float)_qualityLevel) / 100.0f);
HRESULT.Check(UnsafeNativeMethods.IPropertyBag2.Write(
encoderOptions,
1,
ref propBag,
ref propValue));
}
finally
{
propBag.Clear();
propValue.Clear();
}
}
HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.Initialize(
frameEncodeHandle,
encoderOptions
));
}
///
/// Set a lossless rotation value of 90 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate90
{
get
{
return ((Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate90) && (!Rotate270)));
}
set
{
if (value != this.Rotate90)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate90;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
///
/// Set a lossless rotation value of 180 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate180
{
get
{
return ((Convert.ToBoolean((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate180) && (!Rotate270)));
}
set
{
if (value != this.Rotate180)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate180;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
///
/// Set a lossless rotation value of 270 degrees.
/// This replaces any previous lossless transformation.
/// We can coexist with Flip operation
///
private bool Rotate270
{
get
{
return (Convert.ToBoolean(((int)_transformation & (int)WICBitmapTransformOptions.WICBitmapTransformRotate270) == (int)WICBitmapTransformOptions.WICBitmapTransformRotate270));
}
set
{
if (value != this.Rotate270)
{
bool IsFlipH = FlipHorizontal;
bool IsFlipV = FlipVertical;
if (value)
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate270;
}
else
{
_transformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
}
FlipHorizontal = IsFlipH;
FlipVertical = IsFlipV;
}
}
}
#endregion
#region Internal Abstract
/// Need to implement this to derive from the "sealed" object
internal override void SealObject()
{
throw new NotImplementedException();
}
#endregion
#region Data Members
///
/// Critical - CLSID used for creation of critical resources
///
[SecurityCritical]
private Guid _containerFormat = MILGuidData.GUID_ContainerFormatJpeg;
// This happens to be the default used by the jpeg lib.
private const int c_defaultQualityLevel = 75;
private int _qualityLevel = c_defaultQualityLevel;
private const WICBitmapTransformOptions c_defaultTransformation = WICBitmapTransformOptions.WICBitmapTransformRotate0;
private WICBitmapTransformOptions _transformation = c_defaultTransformation;
#endregion
}
#endregion // JpegBitmapEncoder
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DbConnectionPoolOptions.cs
- EdmValidator.cs
- Vector3DCollection.cs
- ObjectSpanRewriter.cs
- NewArrayExpression.cs
- DbConnectionInternal.cs
- UserControlAutomationPeer.cs
- SmiEventSink_DeferedProcessing.cs
- SettingsProviderCollection.cs
- UnaryNode.cs
- Viewport2DVisual3D.cs
- MenuScrollingVisibilityConverter.cs
- PanelContainerDesigner.cs
- SqlUdtInfo.cs
- TextEditorThreadLocalStore.cs
- LogEntryHeaderSerializer.cs
- KeyValuePair.cs
- SoapAttributeAttribute.cs
- EntityTypeEmitter.cs
- HttpHandlerActionCollection.cs
- DbConnectionPool.cs
- ScriptingProfileServiceSection.cs
- ActiveDesignSurfaceEvent.cs
- WebScriptServiceHostFactory.cs
- QueryStringParameter.cs
- PolicyImporterElementCollection.cs
- QueryModel.cs
- SafeReversePInvokeHandle.cs
- ViewStateModeByIdAttribute.cs
- SqlOuterApplyReducer.cs
- InlinedLocationReference.cs
- XhtmlBasicPanelAdapter.cs
- CapabilitiesAssignment.cs
- IRCollection.cs
- ThicknessKeyFrameCollection.cs
- FrameworkElementAutomationPeer.cs
- WindowsAltTab.cs
- ValidationSummary.cs
- FamilyMap.cs
- TextMetrics.cs
- OleDbConnectionFactory.cs
- AxisAngleRotation3D.cs
- DataGrid.cs
- ADMembershipProvider.cs
- SafeSecurityHandles.cs
- Peer.cs
- diagnosticsswitches.cs
- ToolBar.cs
- KnownTypes.cs
- TraceHandler.cs
- ItemMap.cs
- InProcStateClientManager.cs
- GridViewRow.cs
- ErrorWrapper.cs
- ReadonlyMessageFilter.cs
- WebPartVerbsEventArgs.cs
- TextRange.cs
- PerspectiveCamera.cs
- TabletCollection.cs
- BStrWrapper.cs
- DelegateSerializationHolder.cs
- XmlTextReaderImplHelpers.cs
- ConsoleTraceListener.cs
- BmpBitmapDecoder.cs
- BulletChrome.cs
- TimerElapsedEvenArgs.cs
- EncodingStreamWrapper.cs
- ReadOnlyCollectionBase.cs
- SerializationSectionGroup.cs
- GridViewDeleteEventArgs.cs
- CanExecuteRoutedEventArgs.cs
- ParameterCollection.cs
- MemberNameValidator.cs
- ExpressionBuilder.cs
- RichTextBoxDesigner.cs
- DBCommandBuilder.cs
- MenuItem.cs
- DoubleAverageAggregationOperator.cs
- ResourceFallbackManager.cs
- HttpValueCollection.cs
- SqlExpressionNullability.cs
- FtpCachePolicyElement.cs
- KeyValueSerializer.cs
- BindingManagerDataErrorEventArgs.cs
- StorageAssociationTypeMapping.cs
- _ListenerRequestStream.cs
- CheckBoxList.cs
- LocationReferenceEnvironment.cs
- ListBindingHelper.cs
- WeakReferenceEnumerator.cs
- RuleSettings.cs
- TypedOperationInfo.cs
- WorkerRequest.cs
- SmtpFailedRecipientsException.cs
- PatternMatcher.cs
- ObjectItemConventionAssemblyLoader.cs
- PageContentCollection.cs
- PerformanceCounterManager.cs
- DomNameTable.cs
- RuntimeWrappedException.cs