Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:06:56+00:00 2026-05-17T01:06:56+00:00

I work on a WPF application that has multiple canvases and lots of buttons.

  • 0

I work on a WPF application that has multiple canvases and lots of buttons. The user cand load images to change the button background.

This is the code where I load the image in the BitmapImage object

bmp = new BitmapImage();
bmp.BeginInit();
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.UriSource = new Uri(relativeUri, UriKind.Relative);
bmp.EndInit();

and on EndInit() application’s memory grows very much.

One thing that makes thinks better (but doesn’t really fix the problem) is adding

bmp.DecodePixelWidth = 1024;

1024 – my maximum canvas size. But I should do this only to the images with width greater than 1024 – so how can I get the width before EndInit() ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:06:57+00:00Added an answer on May 17, 2026 at 1:06 am

    By loading the image into a BitmapFrame I think you’ll get by with just reading the metadata.

    private Size GetImageSize(Uri image)
    {
        var frame = BitmapFrame.Create(image);
        // You could also look at the .Width and .Height of the frame which 
        // is in 1/96th's of an inch instead of pixels
        return new Size(frame.PixelWidth, frame.PixelHeight);
    }
    

    And then you can do the following when loading the BitmapSource:

    var img = new Uri(ImagePath);
    var size = GetImageSize(img);
    var source = new BitmapImage();
    source.BeginInit();
    if (size.Width > 1024)
        source.DecodePixelWidth = 1024;
    source.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    source.CacheOption = BitmapCacheOption.OnLoad;
    source.UriSource = new Uri(ImagePath);
    source.EndInit();
    myImageControl.Source = source;
    

    I tested this a few times and looked at the memory consumption in the Task Manager and the difference was massive (on a 10MP photo, I saved almost 40MB of private memory by loading it @ 1024 instead of 4272 pixels width)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application that allows users to manipulate multiple images by
I am writing a WPF application that has an optional dependency on an API
Im writing a WPF application that has a zoom and pan ability, but what
At work, we have multiple branches that we may be working on at any
At work I need to create a new web application, that will connect to
We are currently designing a business application that has two primary requirements for it's
I have an application that consists of a WPF UI with an infrastructure layer
Any ideas re why the WPF application I've starting building has a larger width
I am using MVVM architecture to develop a WPF application... So far everything has
What does InitializeComponent() do, and how does it work in WPF? In general first,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.