-1

UPDATE: Is not a duplicate.

My question is not how to take a screenshot. I know this, but it's not what I need. I can have others controls over the Canvas and I don't want to capture them but just the image in the Canvas with the image that is visible in transparency under the Canvas.

I need to capture the image on a Canvas with opacity 50% but the image captured have a black background instead of the image in background (Desktop).

I use this code to capture the image:

MemoryStream ms = new MemoryStream();
RenderTargetBitmap rtb = new RenderTargetBitmap(1920, 1080, 96d, 96d, PixelFormats.Default);
rtb.Render(canvasCapture);
BmpBitmapEncoder BmpEncoder = new BmpBitmapEncoder();
BmpEncoder.Frames.Add(BitmapFrame.Create(rtb));
BmpEncoder.Save(ms);

This is the Canvas:

<Canvas x:Name="canvasCapture" Background="#7F0F0F0E" />

On my application I can see the Desktop in transparency under the Canvas but the captured image have a black background. How Can I capture the image with the background as I see it in the application?

user2272143
  • 469
  • 5
  • 22
  • BMP does not support transparency. Try PNG. – Clemens Oct 03 '19 at 14:00
  • From your comment below: _"I need to capture a Bitmap because I need to encode it in a video using AForge"_ -- AForge supports PNG. As for capturing the image with the background, rather than transparent pixels, that depends on how and what you're capturing. If you render the visual directly within the WPF program, that will only render the things that the WPF program knows about. That doesn't include the desktop background. If you want to include the desktop background, you need to use something that will capture the actual _screen_ pixels. See marked duplicates. – Peter Duniho Oct 03 '19 at 17:15
  • @PeterDuniho My question is not how to take a screenshot. I know this, But it's not what I need. I can have others controls over the Canvas and I don't want to capture them but just the image in the Canvas with the image that is visible in transparency under the Canvas. – user2272143 Oct 03 '19 at 17:55
  • @PeterDuniho PS. I have tied to encode the video with PNG but when I play it I get an error Unsupported format. By the way it is not relevant if I save a BMP or PNG since as I said I need to capture the image under the Canvas and not an empty transparent background. – user2272143 Oct 03 '19 at 18:09
  • _"My question is not how to take a screenshot"_ -- of course it is. You have specifically stated that you want the bitmap to include the image of the desktop background behind your transparent window. The only way to do this is to capture a screenshot. You may then have to crop the result, if there are other elements on the screen you don't want included. But you have to start with the screenshot. – Peter Duniho Oct 04 '19 at 05:26
  • @PeterDuniho Crop the captured image of a screenshot don't remove controls placed over the canvas (I said OVER not inside). If I render only the content of the Canvas all Controls over (not inside) it are not rendered but if I take a screenshot those controls are rendered. I have toolbars and dialogs that are shown while the user use the application and those have not to be visible in the video. – user2272143 Oct 04 '19 at 11:40
  • **Note to those who might reopen this: this absolutely is a duplicate. Please read the OP's requirements before you click that "reopen" link**. _"I have toolbars and dialogs that are shown while the user use the application and those have not to be visible in the video"_ -- then you will have to temporarily made them not visible (i.e. set to `Visibility.Hidden`) when you take the screen shot. Or, you can abandon your requirement that the desktop background be included in the image. Either way, the question you asked is a duplicate of the one that is marked above. – Peter Duniho Oct 04 '19 at 15:18
  • @PeterDuniho If one can't understand a question should not impose the answer. Again I need to capture only what is inside the Canvas NOT the screen. I cannot hide the toolbar when the user need to use it and the video have to be recorded in the same time. To be more precise What I need is a solution to capture the visible pixels only inside the Canvas and not to capture an area of the screen. – user2272143 Oct 04 '19 at 17:05
  • Again, **your stated requirement is that you capture a screen shot**. From your question: _"the image captured have a black background instead of the image in background (Desktop)"_. There is no practical way to obtain _"the image in background"_ other than to get a screenshot. Your refusal to accept this truth is only harming yourself. – Peter Duniho Oct 04 '19 at 17:14
  • @PeterDuniho BTW We are working on a GDI approach to capture only the screenshot of the content of the Canvas- Again the CONTENT not the screen area. When we finished I'll post the solution so you can see that if you don't know something it don't mean it is not possible. – user2272143 Oct 04 '19 at 17:38
  • _"We are working on a GDI approach to capture only the screenshot of the content of the Canvas"_ -- a screenshot, even if just part of the screen, is still a screenshot. So I find it remarkable that you would on the one hand state that your current approach to solve your problem is to capture a screenshot, and then on the other hand claim that your question isn't about capturing a screenshot. – Peter Duniho Oct 04 '19 at 17:40
  • @PeterDuniho It is clear to you the difference between an area of the screen and the content of a control? Well it is probably not the correct way to call it a screenshot, I should say "to capture the visible pixels of the content of the Canvas" that is not the same as render the content of the Canvas – user2272143 Oct 04 '19 at 17:42

1 Answers1

0

Bitmap images don't support transparent backgrounds. It automatically fills transparent areas with black. You may try to save and use your images as PNG.

Emre Ates
  • 1
  • 2
  • Thanks for your answer. I don't need to have an image with transparency but to capture the image with the background that I see in transparency. Also I need to capture a Bitmap because I need to encode it in a video using AForge. – user2272143 Oct 03 '19 at 14:15