BitmapSource Class

Definition

表示在一定大小和分辨率下的单个恒定像素集。Represents a single, constant set of pixels at a certain size and resolution.

public ref class BitmapSource abstract : System::Windows::Media::ImageSource
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public abstract class BitmapSource : System.Windows.Media.ImageSource
type BitmapSource = class
    inherit ImageSource
type BitmapSource = class
    inherit ImageSource
    interface DUCE.IResource
Public MustInherit Class BitmapSource
Inherits ImageSource
Inheritance
Derived
Attributes

Examples

下面的代码示例演示如何创建 BitmapSource,并将其用作 Image 控件的源。The following code example demonstrates how to create a BitmapSource and use it as the source of an Image control.

// Define parameters used to create the BitmapSource.
PixelFormat pf = PixelFormats.Bgr32;
int width = 200;
int height = 200;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];

// Initialize the image with data.
Random value = new Random();
value.NextBytes(rawImage);

// Create a BitmapSource.
BitmapSource bitmap = BitmapSource.Create(width, height,
    96, 96, pf, null,
    rawImage, rawStride);

// Create an image element;
Image myImage = new Image();
myImage.Width = 200;
// Set image source.
myImage.Source = bitmap;
' Define parameters used to create the BitmapSource.
Dim pf As PixelFormat = PixelFormats.Bgr32
Dim width As Integer = 200
Dim height As Integer = 200
Dim rawStride As Integer = CType((width * pf.BitsPerPixel + 7) / 8, Integer)
Dim rawImage(rawStride * height) As Byte

' Initialize the image with data.
Dim value As New Random()
value.NextBytes(rawImage)

' Create a BitmapSource.
Dim bitmap As BitmapSource = BitmapSource.Create(width, height, 96, 96, pf, Nothing, rawImage, rawStride)

' Create an image element;
Dim myImage As New Image()
myImage.Width = 200
' Set image source.
myImage.Source = bitmap

下面的代码示例使用 BitmapSource 派生类 BitmapImage从图像文件创建位图,并将其用作 Image 控件的源。The following code example uses a BitmapSource derived class, BitmapImage, to create a bitmap from an image file and use it as the source of an Image control.

// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);

// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
' Create the image element.
Dim simpleImage As New Image()
simpleImage.Width = 200
simpleImage.Margin = New Thickness(5)

' Create source.
Dim bi As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit()
bi.UriSource = New Uri("/sampleImages/cherries_larger.jpg", UriKind.RelativeOrAbsolute)
bi.EndInit()
' Set the image source.
simpleImage.Source = bi

Remarks

BitmapSourceWindows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 图像处理管道的基本构建基块,从概念上讲,以特定大小和分辨率指定一组固定的像素。BitmapSource is the basic building block of the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) imaging pipeline, conceptually representing a single, constant set of pixels at a certain size and resolution. BitmapSource 可以是解码器提供的图像文件中的单个帧,也可以是操作自身 BitmapSource 的转换的结果。A BitmapSource could be a single frame in an image file that a decoder provides, or it could be the result of a transform that operates on a BitmapSource of its own. BitmapSource 不用于表示多帧图像或动画。BitmapSource is not used to represent a multi-frame image or an animation.

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 本机支持 位图 (BMP)bitmap (BMP)图形交换格式 (GIF)Graphics Interchange Format (GIF)联合图像专家组 (JPEG)Joint Photographics Experts Group (JPEG)可移植网络图形 (PNG)Portable Network Graphics (PNG)标记图像文件格式 (TIFF)Tagged Image File Format (TIFF) 映像的压缩和解压缩。natively supports compression and decompression of 位图 (BMP)bitmap (BMP), 图形交换格式 (GIF)Graphics Interchange Format (GIF), 联合图像专家组 (JPEG)Joint Photographics Experts Group (JPEG), 可移植网络图形 (PNG)Portable Network Graphics (PNG), and 标记图像文件格式 (TIFF)Tagged Image File Format (TIFF) images.

对于位图解码方案,BitmapSource 会根据用户系统上安装的编解码器使用自动编解码器发现。For bitmap decoding scenarios, BitmapSource uses automatic codec discovery, based on the installed codecs on the user's system.

图像的最大高度和宽度是 2 ^ 16 像素,每通道32位,4通道。The maximum height and width of an image is 2^16 pixels at 32 bits per channel * 4 channels. BitmapSource 的最大大小为 2 ^ 32 字节(64 gb),最大图像大小为四个 gigapixels。The maximum size of a BitmapSource is 2^32 bytes (64 gigabytes) and the maximum image size is four gigapixels. 最小图像大小为1x1。The minimum image size is 1x1.

Constructors

BitmapSource()

初始化 BitmapSource 类的新实例。Initializes a new instance of the BitmapSource class.

Properties

CanFreeze

获取一个值,该值指示是否可将对象变为不可修改。Gets a value that indicates whether the object can be made unmodifiable.

(Inherited from Freezable)
DependencyObjectType

获取对此实例的 CLRCLR 类型进行包装的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.

(Inherited from DependencyObject)
Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
DpiX

获取图像的水平每英寸点数 (dpi)dots per inch (dpi)Gets the horizontal 每英寸点数 (dpi)dots per inch (dpi) of the image.

DpiY

获取图像的垂直每英寸点数 (dpi)dots per inch (dpi)Gets the vertical 每英寸点数 (dpi)dots per inch (dpi) of the image.

Format

获取位图数据的本机 PixelFormatGets the native PixelFormat of the bitmap data.

HasAnimatedProperties

获取一个值,该值指示一个或多个 AnimationClock 对象是否与此对象的任何依赖项属性相关联。Gets a value that indicates whether one or more AnimationClock objects is associated with any of this object's dependency properties.

(Inherited from Animatable)
Height

获取源位图的高度(以与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)为单位)。Gets the height of the source bitmap in 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit).

IsDownloading

获取一个值,该值指示 BitmapSource 内容当前是否正在下载。Gets a value that indicates whether the BitmapSource content is currently downloading.

IsFrozen

获取一个值,该值指示对象当前是否可修改。Gets a value that indicates whether the object is currently modifiable.

(Inherited from Freezable)
IsSealed

获取一个值,该值指示此实例当前是否为密封的(只读)。Gets a value that indicates whether this instance is currently sealed (read-only).

(Inherited from DependencyObject)
Metadata

获取与此位图图像关联的元数据。Gets the metadata that is associated with this bitmap image.

Palette

获取位图的调色板(如果指定了调色板)。Gets the color palette of the bitmap, if one is specified.

PixelHeight

获取位图的高度(以像素为单位)。Gets the height of the bitmap in pixels.

PixelWidth

获取位图的宽度(以像素为单位)。Gets the width of the bitmap in pixels.

Width

获取位图的宽度(以与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit)为单位)。Gets the width of the bitmap in 与设备无关的单位(每个单位 1/96 英寸)device-independent units (1/96th inch per unit).

Methods

ApplyAnimationClock(DependencyProperty, AnimationClock)

AnimationClock 应用到指定的 DependencyPropertyApplies an AnimationClock to the specified DependencyProperty. 如果该属性已进行动画处理,则使用 SnapshotAndReplace 切换行为。If the property is already animated, the SnapshotAndReplace handoff behavior is used.

(Inherited from Animatable)
ApplyAnimationClock(DependencyProperty, AnimationClock, HandoffBehavior)

AnimationClock 应用到指定的 DependencyPropertyApplies an AnimationClock to the specified DependencyProperty. 如果该属性已进行动画处理,则使用指定的 HandoffBehaviorIf the property is already animated, the specified HandoffBehavior is used.

(Inherited from Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline)

将动画应用于指定 DependencyPropertyApplies an animation to the specified DependencyProperty. 动画会在呈现下一帧时启动。The animation is started when the next frame is rendered. 如果指定属性已进行动画处理,则使用 SnapshotAndReplace 切换行为。If the specified property is already animated, the SnapshotAndReplace handoff behavior is used.

(Inherited from Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

将动画应用于指定 DependencyPropertyApplies an animation to the specified DependencyProperty. 动画会在呈现下一帧时启动。The animation is started when the next frame is rendered. 如果指定的属性已进行动画处理,则使用指定的 HandoffBehaviorIf the specified property is already animated, the specified HandoffBehavior is used.

(Inherited from Animatable)
CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
CheckIfSiteOfOrigin()

检查位图源内容是否来自已知源的站点。Checks whether the bitmap source content is from a known site of origin. 此方法用于确保像素复制操作的安全。This method is used to make sure that pixel copying operations are safe.

ClearValue(DependencyProperty)

清除属性的本地值。Clears the local value of a property. 要清除的属性由 DependencyProperty 标识符指定。The property to be cleared is specified by a DependencyProperty identifier.

(Inherited from DependencyObject)
ClearValue(DependencyPropertyKey)

清除只读属性的本地值。Clears the local value of a read-only property. 要清除的属性由 DependencyPropertyKey 指定。The property to be cleared is specified by a DependencyPropertyKey.

(Inherited from DependencyObject)
Clone()

创建此 BitmapSource 的可修改克隆,从而深度复制此对象的值。Creates a modifiable clone of this BitmapSource, making deep copies of this object's values. 在复制依赖项属性时,此方法会复制资源引用和数据绑定(但可能不再解析),但不复制动画或其当前值。When copying dependency properties, this method copies resource references and data bindings (but they might no longer resolve) but not animations or their current values.

CloneCore(Freezable)

使此实例成为指定 BitmapSource 的深层副本。Makes this instance a deep copy of the specified BitmapSource. 在复制依赖项属性时,此方法会复制资源引用和数据绑定(但可能不再解析),但不复制动画或其当前值。When copying dependency properties, this method copies resource references and data bindings (but they might no longer resolve) but not animations or their current values.

CloneCurrentValue()

创建此 BitmapSource 对象的可修改复本,从而深度复制此对象的当前值。Creates a modifiable clone of this BitmapSource object, making deep copies of this object's current values. 不复制资源引用、数据绑定和动画,而是复制其当前值。Resource references, data bindings, and animations are not copied, but their current values are.

CloneCurrentValueCore(Freezable)

使用当前的属性值使该实例成为指定 BitmapSource 的可修改深层副本。Makes this instance a modifiable deep copy of the specified BitmapSource using current property values. 不复制资源引用、数据绑定和动画,而是复制其当前值。Resource references, data bindings, and animations are not copied, but their current values are.

CoerceValue(DependencyProperty)

对指定依赖属性的值进行强制。Coerces the value of the specified dependency property. 通过对调用方 DependencyObject 上存在的依赖属性的属性元数据中所指定的任何 CoerceValueCallback 函数进行调用来完成此操作。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

(Inherited from DependencyObject)
CopyPixels(Array, Int32, Int32)

将位图像素数据复制到具有从指定偏移量开始的指定跨距的像素数组中。Copies the bitmap pixel data into an array of pixels with the specified stride, starting at the specified offset.

CopyPixels(Int32Rect, Array, Int32, Int32)

将指定矩形中的位图像素数据复制到具有从指定偏移量开始的指定跨距的像素数组中。Copies the bitmap pixel data within the specified rectangle into an array of pixels that has the specified stride starting at the specified offset.

CopyPixels(Int32Rect, IntPtr, Int32, Int32)

复制指定矩形中的位图像素数据。Copies the bitmap pixel data within the specified rectangle.

Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, Array, Int32)

从像素数组中创建一个新的 BitmapSourceCreates a new BitmapSource from an array of pixels.

Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, IntPtr, Int32, Int32)

从存储在非托管内存中的像素数组创建一个新的 BitmapSourceCreates a new BitmapSource from an array of pixels that are stored in unmanaged memory.

CreateInstance()

初始化 Freezable 类的新实例。Initializes a new instance of the Freezable class.

(Inherited from Freezable)
CreateInstanceCore()

在派生类中实现时,创建 Freezable 派生类的新实例。When implemented in a derived class, creates a new instance of the Freezable derived class.

(Inherited from Freezable)
Equals(Object)

确定提供的 DependencyObject 是否等效于当前 DependencyObjectDetermines whether a provided DependencyObject is equivalent to the current DependencyObject.

(Inherited from DependencyObject)
Freeze()

使当前对象不可修改,并且将其 IsFrozen 属性设置为 trueMakes the current object unmodifiable and sets its IsFrozen property to true.

(Inherited from Freezable)
FreezeCore(Boolean)

使 BitmapSource 实例或派生类为不可变的。Makes an instance of BitmapSource or a derived class immutable.

GetAnimationBaseValue(DependencyProperty)

返回指定的 DependencyProperty 的未经过动画处理的值。Returns the non-animated value of the specified DependencyProperty.

(Inherited from Animatable)
GetAsFrozen()

使用基(未经过动画处理的)属性值创建 Freezable 的冻结副本。Creates a frozen copy of the Freezable, using base (non-animated) property values. 由于副本已冻结,因此将通过引用复制任何冻结的子对象。Because the copy is frozen, any frozen sub-objects are copied by reference.

(Inherited from Freezable)
GetAsFrozenCore(Freezable)

使此实例成为指定的 BitmapSource 对象的克隆。Makes this instance a clone of the specified BitmapSource object.

GetCurrentValueAsFrozen()

使用当前属性值创建 Freezable 的冻结副本。Creates a frozen copy of the Freezable using current property values. 由于副本已冻结,因此将通过引用复制任何冻结的子对象。Because the copy is frozen, any frozen sub-objects are copied by reference.

(Inherited from Freezable)
GetCurrentValueAsFrozenCore(Freezable)

使此实例成为指定 BitmapSource 的冻结复本。Makes this instance a frozen clone of the specified BitmapSource. 不复制资源引用、数据绑定和动画,而是复制其当前值。Resource references, data bindings, and animations are not copied, but their current values are.

GetHashCode()

获取此 DependencyObject 的哈希代码。Gets a hash code for this DependencyObject.

(Inherited from DependencyObject)
GetLocalValueEnumerator()

创建一个专用的枚举数,用于确定哪些依赖项属性在此 DependencyObject 上具有以本地方式设置的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(Inherited from DependencyObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetValue(DependencyProperty)

DependencyObject 的此实例返回依赖属性的当前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(Inherited from DependencyObject)
InvalidateProperty(DependencyProperty)

重新评估指定依赖属性的有效值。Re-evaluates the effective value for the specified dependency property.

(Inherited from DependencyObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnChanged()

修改当前 Freezable 对象时调用。Called when the current Freezable object is modified.

(Inherited from Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject)

确保为刚刚设置的 DependencyObjectType 数据成员建立适当的上下文指针。Ensures that appropriate context pointers are established for a DependencyObjectType data member that has just been set.

(Inherited from Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

此成员支持 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基础结构,但不能在代码中直接使用。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

(Inherited from Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

重写 OnPropertyChanged(DependencyPropertyChangedEventArgs)DependencyObject 实现,以同时调用任何响应类型 Freezable 不断变化的依赖属性的 Changed 处理程序。Overrides the DependencyObject implementation of OnPropertyChanged(DependencyPropertyChangedEventArgs) to also invoke any Changed handlers in response to a changing dependency property of type Freezable.

(Inherited from Freezable)
ReadLocalValue(DependencyProperty)

如果存在,则返回依赖属性的本地值。Returns the local value of a dependency property, if it exists.

(Inherited from DependencyObject)
ReadPreamble()

确保正在从有效的线程访问 FreezableEnsures that the Freezable is being accessed from a valid thread. Freezable 的继承者必须在任何 API 一开始读取不属于依赖项对象的数据成员时调用此方法。Inheritors of Freezable must call this method at the beginning of any API that reads data members that are not dependency properties.

(Inherited from Freezable)
SetCurrentValue(DependencyProperty, Object)

设置依赖属性的值而不更改其值源。Sets the value of a dependency property without changing its value source.

(Inherited from DependencyObject)
SetValue(DependencyProperty, Object)

设置依赖属性的本地值,该值由其依赖属性标识符指定。Sets the local value of a dependency property, specified by its dependency property identifier.

(Inherited from DependencyObject)
SetValue(DependencyPropertyKey, Object)

设置一个只读依赖属性的本地值,该值由依赖属性的 DependencyPropertyKey 标识符指定。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(Inherited from DependencyObject)
ShouldSerializeProperty(DependencyProperty)

返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(Inherited from DependencyObject)
ToString()

基于当前区域性创建此对象的字符串表示形式。Creates a string representation of this object based on the current culture.

(Inherited from ImageSource)
ToString(IFormatProvider)

基于传入的 IFormatProvider 创建此对象的字符串表示形式。Creates a string representation of this object based on the IFormatProvider passed in. 如果提供程序为 null,则使用 CurrentCultureIf the provider is null, the CurrentCulture is used.

(Inherited from ImageSource)
VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
WritePostscript()

引发 FreezableChanged 事件并调用其 OnChanged() 方法。Raises the Changed event for the Freezable and invokes its OnChanged() method. Freezable 派生的类应在修改的类成员不存储为依赖属性的任何 API 的末尾调用此方法。Classes that derive from Freezable should call this method at the end of any API that modifies class members that are not stored as dependency properties.

(Inherited from Freezable)
WritePreamble()

验证 Freezable 是否未被冻结,并且是否正在从有效的线程上下文中访问它。Verifies that the Freezable is not frozen and that it is being accessed from a valid threading context. Freezable 的继承项应当在任何 API 一开始写入不属于依赖项属性的数据成员时调用此方法。Freezable inheritors should call this method at the beginning of any API that writes to data members that are not dependency properties.

(Inherited from Freezable)

Events

Changed

在修改 Freezable 或其包含的对象时发生。Occurs when the Freezable or an object it contains is modified.

(Inherited from Freezable)
DecodeFailed

在由于图像标题损坏而无法下载图像时发生。Occurs when the image fails to load, due to a corrupt image header.

DownloadCompleted

在下载完位图内容时发生。Occurs when the bitmap content has been completely downloaded.

DownloadFailed

在无法下载位图内容时发生。Occurs when the bitmap content failed to download.

DownloadProgress

在下载位图内容的进度有变化时发生。Occurs when the download progress of the bitmap content has changed.

Explicit Interface Implementations

IFormattable.ToString(String, IFormatProvider)

使用指定格式对当前实例的值设置格式。Formats the value of the current instance using the specified format.

(Inherited from ImageSource)

Applies to

See also