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

  • SEARCH
  • Home
  • 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 3783088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:10:29+00:00 2026-05-19T11:10:29+00:00

Hi I trying to scale a png image with a transparent background. I need

  • 0

Hi I trying to scale a png image with a transparent background. I need it to be 250×250 pixel.

Horizontal and vertical centered and keeping the correct aspect ration. The possibility to set a margin.

This is what I got so far.

var img = new System.Windows.Controls.Image();
var bi = new BitmapImage(new Uri("C://tmp/original.png", UriKind.RelativeOrAbsolute));
img.Stretch = Stretch.Uniform;
img.Width = 250;
img.Height = 250;
img.Source = bi;

var pngBitmapEncoder = new PngBitmapEncoder();

var stream = new FileStream("C://tmp/test3.png", FileMode.Create);

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(img));
pngBitmapEncoder.Save(stream);
stream.Close();

I know that its not using the Image object yet, and therefor just saves the image without scaling it. But I’m having trouble saving the Image object. It gives a compile error of cannot convert from ‘System.Windows.Controls.Image’ to ‘System.Uri’

Hope someone can help me 🙂

EDIT

Updated the code, to the version with the compile error. just changed

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(bi));

to

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(img));

And here is a list of my using

using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Image = System.Windows.Controls.Image;
  • 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-19T11:10:30+00:00Added an answer on May 19, 2026 at 11:10 am

    What you’re doing is similar to zooming in an editor on an image and expecting it to be reflected in the underlying image when you save. What you’ll need to do is create a TransformedBitmap to modify the image and then add that to the Frames.
    e.g.

            var scale = new ScaleTransform(250 / bi.Width, 250 / bi.Height);
            var tb = new TransformedBitmap(bi, scale);
            pngBitmapEncoder.Frames.Add( BitmapFrame.Create(tb));
    

    Update Regarding the aspect Ratio.

    I need it to be 250×250 pixel

    If the source image doesn’t have a 1:1 ratio for height and width the scaling above meets the “I need it be 250X250” but will create distortion.

    To get around this you’ll need to either crop the image or scale the image so that just one dimension is 250 pixels.

    To crop the image you can either use the Clip Property or a CroppedBitmap. To scale just one dimension you just use one dimension to determine the scale e.g. new ScaleTransform(250 / bi.Width, 250 / bi.width);

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

Sidebar

Related Questions

I'm trying to select background image depending on the dpi, but even I can
I have a 3200x1200 png image I am trying to resize it to 200x200
I am trying to scale an image (that is from a sprite sheet) using
I have an image I'm trying to use as a background in Android. The
I'm trying to scale an image down, change the image, then scale it back
I am trying to apply a background-image to a li element. <ul> <li class=maintablink>&nbsp;</li>
Basically i am trying to scale an img with an id=room according to a
I'm trying to scale text from 1 to 5, a sort of zoom in
I'm trying to do scale transformation with glMatrix for WebGL. Scale transformations work nicely
I'm trying to use the scale() transform in SVG and d3. I understand it

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.