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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:23:14+00:00 2026-06-05T01:23:14+00:00

Code : private void Foo(Canvas canvas) { // The content is a bit larger…

  • 0

Code :

private void Foo(Canvas canvas)
{
    // The content is a bit larger...
    Size size = new Size(canvas.ActualWidth * 1.1, canvas.ActualHeight * 1.2);

    // Create a render bitmap and push the surface to it
    RenderTargetBitmap renderBitmap =
        new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height,
        96d,
        96d,
        PixelFormats.Pbgra32
    );
    renderBitmap.Render(canvas);

    // Then copy to clipboard
    Clipboard.SetImage(renderBitmap);
}

What I need :

Render a canvas with transparent background to image, then copy it to clipboard (Quit simple? Not really)

Problem :

When paste, I get a ugly image with black background

Solution 1 :

canvas.Background = new SolidColorBrush(Colors.White);

No. This thick do not work, the background of canvas will not change in the next renderBitmap.Render(canvas);

Instead, I have to use a timer, give WPF some time to change the background, then render it in the tick event of that timer. It works, but unfortunately, the content of canvas is larger than it’s size…so the white background can only cover part of it, still ugly result.
(BTW anyone know why it takes sometime to change the background? I thought it should be changed instantly)

Did I do something wrong? How can I get a white background ex-transparent image in clipboard?

What’s more, I’ve noticed that the background of some PNG images remain white if you paste it into mspaint.exe which do not support alpha channel, but some others become black.

Is there something like, say, alternative color, which is used as background if the place where you paste your image do not support alpha channel? Can we custom it?

Now I rendered another BitmapSource with white content, if there is a way to combine it with the renderBitmap as background, the problem solved, but I don’t know how…

int dWidth = (int)size.Width;
int dHeight = (int)size.Height;
int dStride = dWidth * 4;
byte[] pixels = new byte[dHeight * dStride];
for (int i = 0; i < pixels.Length; i++)
{
    pixels[i] = 0xFF;
}
BitmapSource bg = BitmapSource.Create(
    dWidth,
    dHeight,
    96,
    96,
    PixelFormats.Pbgra32,
    null,
    pixels,
    dStride
);
// Combine bg with renderBitmap
  • 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-06-05T01:23:16+00:00Added an answer on June 5, 2026 at 1:23 am

    Here’s my last solution, hope it would help others with the same problem

    // Create a render bitmap and push the surface to it
    RenderTargetBitmap renderBitmap =
        new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height,
        96d,
        96d,
        PixelFormats.Pbgra32
    );
    renderBitmap.Render(surface);
    
    // Create a white background render bitmap
    int dWidth = (int)size.Width;
    int dHeight = (int)size.Height;
    int dStride = dWidth * 4;
    byte[] pixels = new byte[dHeight * dStride];
    for (int i = 0; i < pixels.Length; i++)
    {
        pixels[i] = 0xFF;
    }
    BitmapSource bg = BitmapSource.Create(
        dWidth,
        dHeight,
        96,
        96,
        PixelFormats.Pbgra32,
        null,
        pixels,
        dStride
    );
    
    // Adding those two render bitmap to the same drawing visual
    DrawingVisual dv = new DrawingVisual();
    DrawingContext dc = dv.RenderOpen();
    dc.DrawImage(bg, new Rect(size));
    dc.DrawImage(renderBitmap, new Rect(size));
    dc.Close();
    
    // Render the result
    RenderTargetBitmap resultBitmap =
        new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height,
        96d,
        96d,
        PixelFormats.Pbgra32
    );
    resultBitmap.Render(dv);
    
    // Copy it to clipboard
    try
    {
        Clipboard.SetImage(resultBitmap);
    } catch { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: [TestMethod] public void TestFoo() { Foo(null); } private void
Check out the following code: private void Foo(object bar) { Type type = bar.GetType();
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
This code is not valid: private void Foo(string optionalString = string.Empty) { // do
here's my code private void make_Book(int x, int y, string name) { #region Creating
I have the following code: private void form1_closing(object sender, FormClosingEventArgs e) { e.Cancel =
I have this code private void writeReport(IReport report, string reportName) { string reportString =
this is my code private void sendToClient(Dictionary<string, string> reportDic) { Response.Clear(); Response.BufferOutput = false;
I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item);
With this code: private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { const int TICKETSOURCE_COLUMN =

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.