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 3431326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:18:44+00:00 2026-05-18T07:18:44+00:00

I am trying to use System.Windows.Media.Imaging to composite two bitmaps of the same size

  • 0

I am trying to use System.Windows.Media.Imaging to composite two bitmaps of the same size and format into a third file of the same size and format. I am doing this outside of the context of WPF (working on the code in LINQPad) as the intention is to work this into a ASP.net application as an alternative to the unsupported System.Drawing.

// load the files
var layerOne = new BitmapImage(new Uri(layerOneFile, UriKind.Absolute));
var layerTwo = new BitmapImage(new Uri(layerTwoFile, UriKind.Absolute));

// create the destination based upon layer one
var composite = new WriteableBitmap(layerOne);

// copy the pixels from layer two on to the destination
int[] pixels = new int[(int)layerTwo.Width * (int)layerTwo.Height];
int stride = (int)(4 * layerTwo.Width);
layerTwo.CopyPixels(pixels, stride, 0);
composite.WritePixels(Int32Rect.Empty, pixels, stride, 0);

// encode the bitmap to the output file
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(composite));
using (var stream = new FileStream(outputFile, FileMode.Create))
{
    encoder.Save(stream);
}

This creates a file that is identical to the file loaded from layerOne, what I was expecting was layerTwo to be overlayed on layerOne. What appears to be happening is the data is been written to the BackBuffer but is never being rendered onto the bitmap… presumably this is something the dispatcher would normally be doing.

Where am I going wrong? and how can I get back on track?

  • 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-18T07:18:45+00:00Added an answer on May 18, 2026 at 7:18 am

    The problem is with the first argument for WritePixels, which indicates the area of the WriteableBitmap to update.

    Instead of Int32Rect.Empty, you can do something like the following and should see the second image written over the first:

    Int32Rect sourceRect = new Int32Rect(0, 0, (int)layerTwo.Width, (int)layerTwo.Height);
    composite.WritePixels(sourceRect, pixels, stride, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the Rect structure from System.Windows in MonoDevelop, but I can't
Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but
Getting system.entrypointnotfoundexception: loadlibrary While trying to use SevenZipLib.dll to uncompress the .7z file containing
I am trying to use System.Net.WebClient in a WinForms application to upload a file
I'm trying to use System.Drawing.RotateFlipType in my settings file. This screenshot summarizes my problem:
I'm trying to use System.Windows.Forms.HTMLDocument in a console application. First, is this even possible?
In Silverlight, I can use System.Windows.Browser.HtmlPage.Navigate(url); to navigate to an HTML page. I'm trying
I'm trying to use the System.Windows.Forms.MonthCalendar control within a VSTO Excel workbook. I want
I'm trying to use a System.Windows.Forms.DataGrid control in my Compact Framework 3.5 Window Mobile
When I trying to use System.Windows.Form.Design namespace, I have to add reference all the

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.