2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

クリップボードの画像ファイルを名前を付けて保存するコンテキストメニューへの追加

Last updated at Posted at 2023-05-07

ソースコード

クリップボードから画像データを取得して、名前を付けて保存する。

ClipImage.cs
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ClipImage
{
    public static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Execute(args);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error");
            }
        }

        static void Execute(string[] args)
        {
            var img = Clipboard.GetImage();
            if (img == null)
            {
                throw new Exception("Empty.");
            }

            var sfd = new SaveFileDialog();
            sfd.FileName = "新しいファイル.jpeg";
            sfd.Filter = "画像ファイル(*.jpeg)|*.jpeg";
            if (args.Length == 0)
            {
                sfd.InitialDirectory = @"C:\";
            }
            else
            {
                sfd.InitialDirectory = args[0];
            }

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                img.Save(sfd.FileName, ImageFormat.Jpeg);
            }
        }
    }
}

コンパイル

csc.exeでコンパイルする。

csc.exe /out:clipimage.exe /target:winexe ClipImage.cs

コンテキストメニューへ追加

以下のレジストリの場所へ

HKEY_CLASSES_ROOT\Directory\Background\shell\IMAGE♪

以下の値を追加

キー名:command
値:"C:\{exeの配置場所}\clipimage.exe" "%V"

※やる前へ念のために、元に戻せるのでレジストリをエクスポートしておくのを推奨

動作

コンテキストメニューに「IMAGE♪」が追加される。
image.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?