0
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 3 years have passed since last update.

Microsoft Teamsのチャットログの画像をクリップボードにコピーしたらHTMLだった件

Posted at

まえがき

Teamsのチャット内容とかを他の管理台帳とかに手軽にコピーしたいと思い、色々調べ始めてみたところ、いきなりつまづいた。

チャットログに貼られた画像をコピーすると・・・

image.png

以前作ったツールを使ってクリップボードの保存形式を調べると・・・HTML Formatしかない!
image.png

中身のデータを取り出すと、


Version:0.9
StartHTML:0000000xxx
EndHTML:0000030xxx
StartFragment:0000000xxx
EndFragment:0000030xxx
<html>
<body>
<!--StartFragment--><img src="data:image/jpeg;base64,/9j/4AAQS (中略) FFFAH/2Q=="><!--EndFragment-->
</body>
</html>

みたいな感じになってます。
(参考: html imgタグ base64 - Google検索

というわけで、クリップボードからこのHTMLの文字列を受け取って、画像に変換して表示するコードを書いてみました。

サンプルソースコード


using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

class SampleForm : Form
{
    PictureBox pct;

    SampleForm()
    {
        pct = new PictureBox(){ Dock = DockStyle.Fill, };

        Controls.Add(pct);
        CopyImageFromClipboardToControl();
    }
    
    void CopyImageFromClipboardToControl()
    {
        IDataObject data = Clipboard.GetDataObject();
        object tmp = data.GetData("HTML Format");

        if ( tmp != null && tmp is string ) {
            string[] lines = ((string)tmp).Split('\n'); // 行単位に分解

            foreach ( string line in lines ) {
                string imgBase64str = GetBase64ImgStr(line);
                if ( imgBase64str != null ) {
                    Bitmap bmp = GetBitmapFromBase64(imgBase64str);
                    pct.Image = bmp;
                    ResizeForm(bmp.Size);
                }
            }
        }
        else {
            Console.WriteLine("No data.");
        }
    }

    string GetBase64ImgStr(string line)
    {
        string targetStr = "<img src=\"data:image/jpeg;base64,";

        int index = line.IndexOf(targetStr);
        if ( index >= 0 ) {
            index += targetStr.Length;
            int indexOfEndQuote = line.IndexOf("\"", index);
            if ( indexOfEndQuote >= 0 ) {
                return line.Substring(index, indexOfEndQuote - index);
            }
        }
        return null;
    }

    Bitmap GetBitmapFromBase64(string base64str)
    {
        byte[] t = Convert.FromBase64String(base64str);
        var ms = new MemoryStream(t);
        return new Bitmap(ms);
    }

    void ResizeForm(Size preferedSize)
    {
        Screen ownerScreen = Screen.FromControl(this); // 所属している画面を取得
        Rectangle rect = ownerScreen.Bounds;

        // 画面に収まるときだけリサイズする
        //(※厳密には、タイトルバーと枠線を考慮に入れていないので収まらない)
        if ( preferedSize.Width <= rect.Width && preferedSize.Width <= rect.Height ) {
            ClientSize = preferedSize;
        }
    }


    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new SampleForm());
    }
}

結果

ちゃんと読み込めました。

image.png

0
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
0
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?