"C#/WPF" modifies the DPI and Resolution of the image

Source: Internet
Author: User

Original: "C#/WPF" modifies the DPI and Resolution of the image

Problem:
The DPI of the image used by default in WPF is 96. If the dpi of the voxel we are using is not 96 o'clock (for example, 72), then WPF will automatically change the DPI of the image to 96, causing the actual size of the images to be loaded, width and height will be larger than desired (the original display size will be 72/96 = 3/4 of the actual figure size), The slice will display a super-box inside the image control.

How to find the problem:
This problem is found in debug from the HorizontalResolution, verticalresolution properties of bitmap. (The resolution should have been 72, but it turned out to be 96). Bitmap changes to BitmapImage will cause the dpi of the original image to change from 72 to 96!

Idea: Try to change the DPI from 96 back to the design 72.

When a WPF image control displays a picture, the control requires a source assignment type of imagesource, which can be used to set a picture for an image control, an inheritance relationship:

BitmapImage--BitmapSource--ImageSource

You can assign a source to the image control even if you have any of the three types. The choice is based on the actual demand, as there is also a conversion between the types to consider.
Because these types are not generic (the BitmapImage class temporarily only sees WPF in use), it is now time to use the generic bitmap type instead. That is, the operation bitmap type, and finally the transformation to BitmapImage (or Bitmapsource/imagesource) to the foreground image control to use. (You can write a translator for a XAML, or a controller layer transformation).

To dynamically modify the dpi of each image through the code, it is found that the DPI-related properties of the BitmapImage class are basically read-only. The HorizontalResolution and VerticalResolution properties on the bitmap class are read-only, but there is a setresolution () method. You can use this method to modify the resolution.

There is a more alternative approach. The BitmapImage body decodepixelwidth and decodepixelheight are readable and writable, and this property also affects the true width of the image display. It is possible to change these two properties to achieve the scaling of the image without changing the dpi. The code is as follows:

publicstaticvoidModifyBitmapImageDecodePixel(BitmapImage bi, System.Drawing.Bitmap bitmap){    double72.096.0// 因为Bitmap转BitmapImage时,DPI从96变成了72,导致图像变大。    bi.DecodePixelWidth = (int)(bitmap.Width * scale);    bi.DecodePixelHeight = (int)(bitmap.Height * scale);}

Summary: Modifying the DPI or resolution can eventually modify the true display size of the image.

This article is only a record of what WPF has so a default image dpi is 96 pit.

"C#/WPF" modifies the DPI and Resolution of the image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.