C#难点记录-剪切透明图到剪切板

下面的C#代码是我和朋友一起解决的,粘贴记录下

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace 演示demo
{
	// Token: 0x02000002 RID: 2
	public class Form1 : Form
	{
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		public Form1()
		{
			this.InitializeComponent();
		}

		// Token: 0x06000002 RID: 2 RVA: 0x00002068 File Offset: 0x00000268
		private void button1_Click(object sender, EventArgs e)
		{
			string path = AppDomain.CurrentDomain.BaseDirectory + "1.png";
			Bitmap bm = new Bitmap(path, false);
			Metafile mf = this.GetGeometryMetafile(bm);
			Form1.ClipboardMetafileHelper.PutEnhMetafileOnClipboard(IntPtr.Zero, mf);
		}

		// Token: 0x06000003 RID: 3 RVA: 0x000020A8 File Offset: 0x000002A8
		public Metafile GetGeometryMetafile(Bitmap bitmap)
		{
			Metafile metafile;
			using (MemoryStream stream = new MemoryStream())
			{
				using (Graphics rtfBoxGraphics = Graphics.FromImage(bitmap))
				{
					IntPtr pDeviceContext = rtfBoxGraphics.GetHdc();
					metafile = new Metafile(stream, pDeviceContext);
					using (Graphics imageGraphics = Graphics.FromImage(metafile))
					{
						imageGraphics.DrawImageUnscaled(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
					}
					rtfBoxGraphics.ReleaseHdc(pDeviceContext);
				}
			}
			return metafile;
		}

		// Token: 0x06000004 RID: 4 RVA: 0x0000215C File Offset: 0x0000035C
		private void button2_Click(object sender, EventArgs e)
		{
		}

		// Token: 0x06000005 RID: 5 RVA: 0x00002160 File Offset: 0x00000360
		protected override void Dispose(bool disposing)
		{
			bool flag = disposing && this.components != null;
			if (flag)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		// Token: 0x06000006 RID: 6 RVA: 0x00002198 File Offset: 0x00000398
		private void InitializeComponent()
		{
			this.button1 = new Button();
			this.button2 = new Button();
			base.SuspendLayout();
			this.button1.Location = new Point(135, 86);
			this.button1.Name = "button1";
			this.button1.Size = new Size(75, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "复制";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += this.button1_Click;
			this.button2.Location = new Point(337, 85);
			this.button2.Name = "button2";
			this.button2.Size = new Size(75, 23);
			this.button2.TabIndex = 1;
			this.button2.Text = "button2";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += this.button2_Click;
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(800, 450);
			base.Controls.Add(this.button2);
			base.Controls.Add(this.button1);
			base.Name = "Form1";
			this.Text = "Form1";
			base.ResumeLayout(false);
		}

		// Token: 0x04000001 RID: 1
		private IContainer components = null;

		// Token: 0x04000002 RID: 2
		private Button button1;

		// Token: 0x04000003 RID: 3
		private Button button2;

		// Token: 0x02000006 RID: 6
		internal static class ClipboardMetafileHelper
		{
			// Token: 0x0600000F RID: 15
			[DllImport("user32.dll")]
			private static extern bool OpenClipboard(IntPtr hWndNewOwner);

			// Token: 0x06000010 RID: 16
			[DllImport("user32.dll")]
			private static extern bool EmptyClipboard();

			// Token: 0x06000011 RID: 17
			[DllImport("user32.dll")]
			private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

			// Token: 0x06000012 RID: 18
			[DllImport("user32.dll")]
			private static extern bool CloseClipboard();

			// Token: 0x06000013 RID: 19
			[DllImport("gdi32.dll")]
			private static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, string fileName);

			// Token: 0x06000014 RID: 20
			[DllImport("gdi32.dll")]
			private static extern bool DeleteEnhMetaFile(IntPtr hemf);

			// Token: 0x06000015 RID: 21 RVA: 0x00002404 File Offset: 0x00000604
			public static bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile metafile)
			{
				return Form1.ClipboardMetafileHelper.PutEnhMetafileOnClipboard(hWnd, metafile, true);
			}

			// Token: 0x06000016 RID: 22 RVA: 0x00002420 File Offset: 0x00000620
			public static bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile metafile, bool clearClipboard)
			{
				bool flag = metafile == null;
				if (flag)
				{
					throw new ArgumentNullException("metafile");
				}
				bool bResult = false;
				IntPtr hEMF = metafile.GetHenhmetafile();
				bool flag2 = !hEMF.Equals(IntPtr.Zero);
				if (flag2)
				{
					try
					{
						IntPtr hEMF2 = Form1.ClipboardMetafileHelper.CopyEnhMetaFile(hEMF, null);
						bool flag3 = !hEMF2.Equals(IntPtr.Zero);
						if (flag3)
						{
							bool flag4 = Form1.ClipboardMetafileHelper.OpenClipboard(hWnd);
							if (flag4)
							{
								try
								{
									if (clearClipboard)
									{
										bool flag5 = !Form1.ClipboardMetafileHelper.EmptyClipboard();
										if (flag5)
										{
											return false;
										}
									}
									bResult = Form1.ClipboardMetafileHelper.SetClipboardData(14U, hEMF2).Equals(hEMF2);
								}
								finally
								{
									Form1.ClipboardMetafileHelper.CloseClipboard();
								}
							}
						}
					}
					finally
					{
						Form1.ClipboardMetafileHelper.DeleteEnhMetaFile(hEMF);
					}
				}
				return bResult;
			}

			// Token: 0x06000017 RID: 23 RVA: 0x00002510 File Offset: 0x00000710
			public static bool SaveEnhMetaFile(string fileName, Metafile metafile)
			{
				bool flag = metafile == null;
				if (flag)
				{
					throw new ArgumentNullException("metafile");
				}
				bool result = false;
				IntPtr hEmf = metafile.GetHenhmetafile();
				bool flag2 = hEmf != IntPtr.Zero;
				if (flag2)
				{
					IntPtr resHEnh = Form1.ClipboardMetafileHelper.CopyEnhMetaFile(hEmf, fileName);
					bool flag3 = resHEnh != IntPtr.Zero;
					if (flag3)
					{
						Form1.ClipboardMetafileHelper.DeleteEnhMetaFile(resHEnh);
						result = true;
					}
					Form1.ClipboardMetafileHelper.DeleteEnhMetaFile(hEmf);
					metafile.Dispose();
				}
				return result;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值