Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paste image from clipboard as base64 #798

Open
NewUserHa opened this issue Aug 28, 2020 · 15 comments
Open

Paste image from clipboard as base64 #798

NewUserHa opened this issue Aug 28, 2020 · 15 comments
Labels
Issue: Feature Something brand new. Needs Discussion We haven't decided what to do. Upstream Pertaining to an upstream component, or blocked by an upstream issue.

Comments

@NewUserHa
Copy link

NewUserHa commented Aug 28, 2020

Proposal

Adding pictures in Markdown is very useful at most time. But the expired or private image URL could be a problem.

We should have a way to keep images in local files:

Paste / Convert image from clipboard or URL to Base64 data URL or local file.

Tasks

VS Code is also interested in this topic. They appear to have another way to solve it.

Our approach (Clipboard API) is blocked by the upstream.

@yzhang-gh
Copy link
Owner

There are already many extensions dealing with Markdown images, e.g. Paste Image. It is more suitable as a feature request for those extensions.

@NewUserHa
Copy link
Author

Cool! Very useful information.

Thanks for your replay.

@NewUserHa
Copy link
Author

NewUserHa commented Aug 28, 2020

But it does not support encoding to base64. mushanshitiancai/vscode-paste-image#74

Shall we reconsider it? The repo seems inactive for some time already

@NewUserHa NewUserHa reopened this Aug 28, 2020
@yzhang-gh
Copy link
Owner

I can leave this open as a feature request and see what others think of it.

@yzhang-gh yzhang-gh added Needs Discussion We haven't decided what to do. Issue: Feature Something brand new. labels Aug 29, 2020
@Lemmingh Lemmingh changed the title [feature request] paste image from clipboard to base64 markdown Paste image from clipboard as base64 Nov 24, 2020
@gavbarnett
Copy link
Contributor

I like this idea.

Paste Image works fine, but it would be nice to name the files after the section headers as well as time-stamped.
Might also be nice to have the key binding as Ctrl+V instead of Ctrl+Alt+V if possible.

Similarly #522 for pasting from excel sounds nice to have.

@gavbarnett
Copy link
Contributor

Generally pasting any file I think should just copy it locally.

sudo code

Pastefunction(paste_object):
   select (paste_object.type)
      case image_type: image_paste() //create image locally (options for file type, size, name)
      case excel_type: excel_paste() //create table
      case file_type: file_paste() //copy file locally
      case ...

@pavlexander
Copy link

For anyone who interested in application-agnostic solution check this script: https://github.com/pavlexander/imageAsBase64/blob/main/powershell/pasteImageAsB64.ps1

Add-Type -Assembly PresentationCore

Add-Type -TypeDefinition '
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyLogger {
  public static class Program {
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static HookProc hookProc = HookCallback;
    private static IntPtr hookId = IntPtr.Zero;
    private static int keyCode = 0;
    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll")]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    [DllImport("user32.dll")]
    private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
    [DllImport("kernel32.dll")]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
    public static int WaitForKey() {
      hookId = SetHook(hookProc);
      Application.Run();
      UnhookWindowsHookEx(hookId);
      return keyCode;
    }
    private static IntPtr SetHook(HookProc hookProc) {
      IntPtr moduleHandle = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
      return SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, moduleHandle, 0);
    }
    private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
      if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) {
        keyCode = Marshal.ReadInt32(lParam);
        Application.Exit();
      }
      return CallNextHookEx(hookId, nCode, wParam, lParam);
    }
  }
}
' -ReferencedAssemblies System.Windows.Forms

function GetImageFromClipboard(){
	$img = get-clipboard -format image
	
	if ($img -eq $null) {
		return ""
	}
	
	$memoryStream = New-Object System.IO.MemoryStream
	$img.save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Png)
	$bytes = $memoryStream.ToArray()
	$memoryStream.Flush()
	$memoryStream.Dispose()
	$iconB64 = [convert]::ToBase64String($bytes)

	return $iconB64
}

while ($true) {
    $key = [System.Windows.Forms.Keys][KeyLogger.Program]::WaitForKey();
	#Write-Host $key
    if ($key -eq "Pause") {
		$b64Image = GetImageFromClipboard
		if (![string]::IsNullOrEmpty($b64Image)) {
			$valueToPrint = "![Img X](data:image/png;base64,${b64Image})"		
			Set-Clipboard -Value $valueToPrint
			[System.Windows.Forms.SendKeys]::SendWait("^{v}") 
			Write-Host 'Sent'
		}
		else {
			Write-Host 'No image found'
		}
    }
}

You can use it with any other application of your choice, not just vscode. Though, I agree that having this functionality as part of applications' standard tooling would be more convenient and bulletproof.

p.s. check the readme for known limitations.

@NULL0B

This comment has been minimized.

@fairking
Copy link

fairking commented Apr 26, 2021

Apart of base64 as an option to provide a local folder (eg. "./images") so all images are saved into the folder when pasted/dragged and dropped. In additional when I paste images (mostly they are screenshots) I ussualy need to rename them. And I really like how it is done in the following VS2019 extension (https://github.com/madskristensen/MarkdownEditor). Or it could be done straight from the md file.

@heartacker
Copy link

really useful if we could paste an image (image file or file clip)to markdown file and save it to folder of /image/${currentFileName}/

@Lemmingh Lemmingh added the Upstream Pertaining to an upstream component, or blocked by an upstream issue. label Aug 27, 2021
@Lemmingh
Copy link
Collaborator

Lemmingh commented Oct 4, 2021

I'm afraid we have to wait for the upstream.

We cannot take any native code, otherwise, #996 will be impaired.

VS Code's Clipboard API only supports text for now due to the limitations of the Web API. (microsoft/vscode#77790)

@voyagerDevil
Copy link

If anyone is interested, I recently made a vscode extension to paste images directly from clipboard to base64, the reason is that some companies still prefer to have the images in the same MD file.
You can check it out in the Marketplace

@keeely
Copy link

keeely commented Nov 3, 2023

If anyone is interested, I recently made a vscode extension to paste images directly from clipboard to base64, the reason is that some companies still prefer to have the images in the same MD file. You can check it out in the Marketplace

Agreed. Having images scattered all over the place can be annoying, it's not so much the adding them, it's the subsequent managing/updating, and dreaming up appropriate names when all you want to do is illustrate some point in a readme with a screenshot.

@keeely
Copy link

keeely commented Nov 3, 2023

I can leave this open as a feature request and see what others think of it.

I've been searching for a free markdown editor that handles base64 encoding on image copy-paste and I don't believe there is a single free one. There are web-based ones: CKEditor, Toast UI editor, but they require some fiddling to create standalone applications. You can usually use the online demos but pasting text into a web page is not always desirable.

Markdown all-in-one seems tantalisingly close to providing this functionality, and even teases us with an option for inline base64 for printing to html. I hope you'll consider adding this as well.

@yzhang-gh
Copy link
Owner

yzhang-gh commented Nov 4, 2023

It looks like there are some interesting extensions e.g., ClipImage64 which can be used in company with this extension. (And Markdown Paste if you do not want base64.)


Oops sorry, just realized it has been mentioned above

If anyone is interested, I recently made a vscode extension to paste images directly from clipboard to base64, the reason is that some companies still prefer to have the images in the same MD file. You can check it out in the Marketplace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Feature Something brand new. Needs Discussion We haven't decided what to do. Upstream Pertaining to an upstream component, or blocked by an upstream issue.
Projects
None yet
Development

No branches or pull requests

10 participants