在IE右键菜单,添加图片保存快捷方式
先用C#做一个COM
作用为下载图片和图片改名(使用GUID)
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Threading;
- [assembly: AssemblyKeyFile("gachal_d_key.snk")]
- namespace PicsDown
- {
- [Guid("694C1820-04B6-4988-928F-FD858B95C880")]
- public interface DownLoad_Interface
- {
- [DispId(1)]
- void Down(string url, string dir);
- [DispId(2)]
- void SavePhotoFromUrl(string FileName, string Url);
- [DispId(3)]
- bool SaveBinaryFile(WebResponse response, string FileName);
- }
- // 事件接口Database_COMObjectEvents
- [Guid("47C976E0-C208-4740-AC42-41212D3C34F0"),
- InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
- public interface DownLoad_Events
- {
- }
- delegate void MyDelegate(string FileName, string Url);
- [Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"),
- ClassInterface(ClassInterfaceType.None),
- ComSourceInterfaces(typeof(DownLoad_Events))]
- public class DownLoad : DownLoad_Interface
- {
- public void Down(string url,string dir)
- {
- string FileName;
- string Url;
- string tempUrl = url.Substring(url.Length - 5, 5);
- tempUrl = tempUrl.Substring(tempUrl.IndexOf(".", 0), 4);
- if (tempUrl == ".jpe")
- {
- tempUrl = ".jpeg";
- }
- FileName = @dir + Guid.NewGuid().ToString() + tempUrl;
- Url = url;
- MyDelegate del = new MyDelegate(this.SavePhotoFromUrl);
- IAsyncResult ar = del.BeginInvoke(FileName, Url, null, null);
- //del.EndInvoke(ar);
- }
- /// <summary>
- /// 从图片地址下载图片到本地磁盘
- /// </summary>
- /// <param name="ToLocalPath">图片本地磁盘地址</param>
- /// <param name="Url">图片网址</param>
- /// <returns></returns>
- public void SavePhotoFromUrl(string FileName, string Url)
- {
- bool Value = false;
- WebResponse response = null;
- Stream stream = null;
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- response = request.GetResponse();
- stream = response.GetResponseStream();
- if (!response.ContentType.ToLower().StartsWith("text/"))
- {
- Value = SaveBinaryFile(response, FileName);
- }
- }
- catch (Exception err)
- {
- string aa = err.ToString();
- }
- }
- /// <summary>
- /// Save a binary file to disk.
- /// </summary>
- /// <param name="response">The response used to save the file</param>
- // 将二进制文件保存到磁盘
- public bool SaveBinaryFile(WebResponse response, string FileName)
- {
- bool Value = true;
- byte[] buffer = new byte[1024];
- try
- {
- if (File.Exists(FileName))
- File.Delete(FileName);
- Stream outStream = System.IO.File.Create(FileName);
- Stream inStream = response.GetResponseStream();
- int l;
- do
- {
- l = inStream.Read(buffer, 0, buffer.Length);
- if (l > 0)
- outStream.Write(buffer, 0, l);
- }
- while (l > 0);
- outStream.Close();
- inStream.Close();
- }
- catch
- {
- Value = false;
- }
- return Value;
- }
- }
- }
在这之前先建立snk文件,sn -k gachal_d_key.snk,在文件放在项目中
注意在VS中,项目->属性->应用程序->信息集信息 中 把程序集COM可见 选择
项目->属性->生成 为COM互操作注册 选择
这里使用了多线程运行SavePhotoFromUrl函数,保证保存图片时不会“卡卡”的
然后在注册表HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer/MenuExt
上添加新项,命名为保存图片(&x),注意X将被设为快捷键
在该项添加一个字符值,名字默认,值为c:/download.htm
再添加一个DWORD值,名字为contexts,值为十六进制的2
在c盘新建名为download.htm的文件,内容如下
- <mce:script language="javascript"><!--
- var objNetSamp=new ActiveXObject("PicsDown.DownLoad");//创建COM DLL对象
- var srcEvent = external.menuArguments.event;
- var EventElement;
- if(typeof(srcEvent.clientX) == "undefined")
- {
- EventElement = external.menuArguments.document.elementFromPoint ( srcEvent.pointerX, srcEvent.pointerY );
- }
- else
- {
- EventElement = external.menuArguments.document.elementFromPoint ( srcEvent.clientX, srcEvent.clientY );
- }
- if(EventElement != null && EventElement.tagName == "IMG")
- {
- objNetSamp.Down(EventElement.src,"C://pics//");
- }
- // --></mce:script>
OK,以后在IE保存图片就直接右键然后按X就会保存到c:/pic中,要改为其他目录只要修改download.htm的文件即可。
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任
本文转自9pc9com博客,原文链接: http://blog.51cto.com/215363/744261
如需转载请自行联系原作者