mushanshitiancai / vscode-paste-image

paste image from clipboard to markdown/asciidoc directly!
MIT License
423 stars 128 forks source link

Black background on images #29

Closed neighthan closed 5 years ago

neighthan commented 5 years ago

I'm getting some weird results from pasting graphs recently. Here's an example image pasted directly into GitHub:

image

And here's a screenshot of what that image looks like when I paste it into a markdown document:

image

Any idea what could be causing this / how to fix it?

neighthan commented 5 years ago

So, the images in question are copy-pasted and have a transparent background. I'm using a workaround now where I explicitly set the background to be white; then things work fine with pasting the image using this extension. So, I guess the problem is how transparent backgrounds are handled? I think using white instead of black for the background color when there is none would make more sense. If this is easy to change, it would be a nice fix. Otherwise, I'm content with my explicitly white instead of transparent background workaround for now.

mushanshitiancai commented 5 years ago

Hello, What operating system do you use?

neighthan commented 5 years ago

Windows 10. I don't know that it matters, but I copy the images out of a Jupyter notebook. The notebook server is running on an Ubuntu machine, but I access the client from Windows.

mushanshitiancai commented 5 years ago

@kivle Hello friend, do you know why?

In file res/pc.ps1:

$img = [Windows.Clipboard]::GetImage()
$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)

It get bitmap from clipboard with Rgb24 mode. Rgb24 it seems without aplha value, so get a black backgroud?

@neighthan Could you run res/pc.ps1(powershell script) in your Win10, and test with Rgb24 and Rgba64?

neighthan commented 5 years ago

Yes, using Rgba64 instead of Rgb24 fixes this issue!

neighthan commented 5 years ago

Okay, the issue seems a bit more complicated. If I copy + paste an image from a Jupyter notebook, it has the black background unless I use Rgba64, then it works. But if I paste in an image that I copied from a screenshot tool, using Rgb24 works and Rgba64 gives an empty image. I assume that the screenshot tool is only copying an RGB image to the clipboard instead of RGBA. Thus a proper solution to this would need to determine when to use each of these - any idea how to do that?

Or, if that's difficult, one workaround might be to have two separate commands that can be bound - one which uses Rgb24 for pasting and the other for Rgba64. Keeping Rgb24 as the default would mean nothing is broken for existing users, and anybody who has RGBA images can then just bind the new command to a different shortcut. One command that's smart about the image encoding would be nicer, but I'd be fine with two commands if that leads to a faster solution.

neighthan commented 5 years ago

I have no Powershell experience, but I used the lines from your script to get images from the clipboard to test on. For both an image that pastes properly with Rgba64 and for one that doesn't, Powershell has the same $img.Format (Bgra32). I tried using Bgra32 instead of Rgba64, but it also gives a blank image. If Powershell says the format for the two images is the same, I'm not sure what's wrong / how to detect which method to use.

mushanshitiancai commented 5 years ago

Hello, thank you for you research.

I use Rgba64 test in my windows, and it's ok for normal png or png with transparent background.

I publish a new version(1.0.3). You can try it!

GutoSchettini commented 3 years ago

Hi. I have this issue, copying from Jupyter Lab to MS-Word...

Please, could someone be more clear about how to fix it? I'm not familiar (or better saying, completely ignorant!) with PowerShell operations... how to change RGB24 to RGBA64?

thanks