需求
- 复制或者截图后按下ALT+V就可以粘贴markdown图片连接,目的是使markdown中的图片不需要因为文档的转移而进行额外转移的操作
功能
- 复制或者截图后按下ALT+V自动将图片上传到githu并获得公网可浏览的图片连接,将图片连接拼接成markdown连接,将拼接后的markdown连接粘贴到光标处
- 可以缩到小图标
- 启动一次程序就自动设置开机运行
前期准备
关键实现
string[] files = new string[Clipboard.GetFileDropList().Count];
Clipboard.GetFileDropList().CopyTo(files, 0);
Clipboard.Clear();
//图片地址 = files[0]
Clipboard.GetImage()
public static string GetBase64(string filePath)
{
try
{
string strPic;
//读图片转为Base64String
System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Path.Combine(filePath));
using (MemoryStream ms1 = new MemoryStream())
{
bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr1 = new byte[ms1.Length];
ms1.Position = 0;
ms1.Read(arr1, 0, (int)ms1.Length);
ms1.Close();
strPic = Convert.ToBase64String(arr1);
}
return strPic;
}
catch (Exception ex)
{
throw ex;
}
}
public static string GetBase64(Image image)
{
try
{
byte[] bt = null;
if (!image.Equals(null))
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(image);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
string strPic = Convert.ToBase64String(bt); //64位图片
return strPic;
}
catch (Exception ex)
{
throw ex;
}
}
public static string Put(string token,string userID,string url, string content)
{
try
{
using (var client = new WebClient())
{
client.Headers.Add("User-Agent", userID); //github用户名
client.Headers.Add("Authorization", "token "+ token);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //允许https请求
var response = client.UploadString(url, "PUT", content); //content 是data
return response;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 调用github上传方法
/// </summary>
/// <param name="strPic">图片base64</param>
/// <param name="PngName">图片名字</param>
private void UploadGitHub(string strPic,string PngName)
{
try
{
var jsonObject1 = new
{
name = userName, //github用户名
email = userEmail //github账号绑定邮箱
};
var jsonObject = new
{
message = "commit from markdown", //提交备注
content = strPic, //图片base64
committer = jsonObject1
};
string jsonStr = JsonConvert.SerializeObject(jsonObject); //序列化为JSON,作为http请求的data参数
Http.Put(token, userName, @url + PngName, jsonStr);
}
catch (Exception ex)
{
throw ex;
}
}
1、代码中的地址是我读配置拼接的,调用完整地址为:
https://api.github.com/repos/用户名/项目名/contents/图片存放文件夹名/图片名
(图片名可以自定义,后缀png)
2、请求头中必须包含User-Agent、Authorization,请求数据data中必须包含name、email、message 、content
fileMDlinks = "![](" + 图片浏览地址 + ")" + "\n";
Clipboard.SetDataObject(fileMDlinks); //把连接地址放入剪切板
System.Windows.Forms.SendKeys.Send("%"); //释放ALT
System.Windows.Forms.SendKeys.Send("^v"); //CTRL+V
拖入notifyIcon控件
//隐藏任务栏区图标
this.Hide();
//图标显示在托盘区
notifyIcon1.Visible = true;
//还原窗体
this.WindowState = FormWindowState.Normal;
this.Show();
//激活窗体并获取焦点
this.Activate();
using IWshRuntimeLibrary;
using System.IO;
using System;
namespace GenerateMarkdown
{
class ShortcutCreator
{
//需要引入IWshRuntimeLibrary,搜索Windows Script Host Object Model
/// <summary>
/// 创建快捷方式
/// </summary>
/// <param name="directory">快捷方式所处的文件夹</param>
/// <param name="shortcutName">快捷方式名称</param>
/// <param name="targetPath">目标路径</param>
/// <param name="description">描述</param>
/// <param name="iconLocation">图标路径,格式为"可执行文件或DLL路径, 图标编号",
/// 例如System.Environment.SystemDirectory + "\\" + "shell32.dll, 165"</param>
/// <remarks></remarks>
public static void CreateShortcut(string directory, string shortcutName, string targetPath,
string description = null, string iconLocation = null)
{
if (!System.IO.Directory.Exists(directory))
{
System.IO.Directory.CreateDirectory(directory);
}
string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
shortcut.TargetPath = targetPath;//指定目标路径
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
shortcut.Description = description;//设置备注
shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
shortcut.Save();//保存快捷方式
}
/// <summary>
/// 创建桌面快捷方式
/// </summary>
/// <param name="shortcutName">快捷方式名称</param>
/// <param name="targetPath">目标路径</param>
/// <param name="description">描述</param>
/// <param name="iconLocation">图标路径,格式为"可执行文件或DLL路径, 图标编号"</param>
/// <remarks></remarks>
public static void CreateShortcutOnDesktop(string shortcutName, string targetPath,
string description = null, string iconLocation = null)
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取桌面文件夹路径
CreateShortcut(desktop, shortcutName, targetPath, description, iconLocation);
}
}
}
ShortcutCreator.CreateShortcut(Directory.GetCurrentDirectory(), "GenerateMarkdownGitHub.exe.lnk", Directory.GetCurrentDirectory() + @"\GenerateMarkdownGitHub.exe", null, null); //创建快捷方式
/// <summary>
/// 设置开机启动
/// </summary>
private void CreateStart()
{
try
{
//设置开机自启动
if (checkBox1.Checked == true)
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("JcShutdown", path);
rk2.Close();
rk.Close();
}
//取消开机自启动
else
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("JcShutdown", false);
rk2.Close();
rk.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
- 也可以从githubapi返回的json数据获取图片浏览地址,要用到json反序列化
Dictionary<string, object> dictList = (Dictionary<string, object>)json.DeserializeObject(json字符串);
如果JSON有多层级,那么还要针对子层级进行强转
(Dictionary<string, object>)dictList[子层级])
项目地址
https://gitee.com/Wolfmoor/GenerateMarkdownGitHub