从浏览器拷贝的图片本地化

yanggensnake 2012-10-10 09:37:27
s场景是这样的,自己开发的一个客户端程序,可以用来保存从浏览器拷贝的网页数据,在浏览器中选中一大段网页,然后复制,粘贴到这个客户端程序,它就会把复制的整个网页保存到本地,现在对图片本地化采用的方式是从剪贴板中获取到所有图片路径,然后到网上将图片下载到本地。这样导致程序保存很慢。
为了提高保存速度,我就想对图片本地化的方式做下改进,目前的考虑是,既然浏览器已经把网页完整的展现出来了,那么图片字节流应该已经存在于本地或者内存中了,如果能够找到这些图片在本地存放的位置,那么就不需要从互联网上再把图片下载下来。不知道这个考虑有没有可行性,请大神们指点一下。或者对图片本地化给些其他的思路~~~
...全文
152 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanggensnake 2012-11-27
  • 打赏
  • 举报
回复
非常感谢~~~~
事理 2012-10-17
  • 打赏
  • 举报
回复


System.Collections.Specialized.StringCollection fileList = Clipboard.GetFileDropList();//获得文件名
if (fileList.Count > 0)
{
cmsMain.Enabled = false;
Thread thread = new Thread(new ParameterizedThreadStart(SaveBrowserPic));
thread.IsBackground = true;
thread.Start(fileList[0]);//复制的图片本地文件路径
}
else if (Clipboard.ContainsImage())
{
Thread thread = new Thread(new ParameterizedThreadStart(SaveBrowserPic2));
thread.IsBackground = true;
thread.Start(Clipboard.GetImage());//保存内存中图片
Clipboard.Clear();
}
else
{
MessageBox.Show("抱歉,无法获取当前图片相关信息!");
}

private void SaveBrowserPic(object filePath)
{
FileInfo fi = new FileInfo(filePath.ToString());
byte[] imageData = new byte[fi.Length];
fs = fi.OpenRead();
fs.Read(imageData, 0, Convert.ToInt32(fi.Length));
}

private void SaveBrowserPic2(object image)
{
int resultCount = 0;
int id = 1;
try
{
Bitmap bmp = (Bitmap)image;

//判断内存图片格式
BitmapData bitmapData = bmp.LockBits(new Rectangle(new Point(0, 0), bmp.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] tempByte = new byte[2];
IntPtr Ptr = bitmapData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(Ptr, tempByte, 0, tempByte.Length);
bmp.UnlockBits(bitmapData);

string imageType1 = GetExtension(tempByte);
tempByte = null;
bitmapData = null;

//将内存图片转换为字节数组
byte[] imageData = null;
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, GetImageFormat(imageType1));
imageData = new byte[ms.Length];
ms.Position = 0;
ms.Read(imageData, 0, Convert.ToInt32(ms.Length));
}
/// <summary>
/// 判断文件后缀名,只支持gif,bmp,jpg,png
/// </summary>
/// <param name="filePath">图片文件路径</param>
/// <returns>后缀名</returns>
private static string GetExtension(byte[] image)
{
string extension = ".jpg";

string fileclass = string.Empty;
byte buffer;
try
{
buffer = image[0];
fileclass = buffer.ToString();
buffer = image[1];
fileclass += buffer.ToString();
}
catch
{
return extension;
}
string[] fileType = { "1029", "7173", "255216", "6677", "13780" };
string[] type = { ".jfif", ".gif", ".jpg", ".bmp", "png" };
for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
extension = type[i];
break;
}
}
return extension;
}



代码没完全给全,但主要的已经给出,自己修改下。
linji8395 2012-10-10
  • 打赏
  • 举报
回复
不错的东东
孟子E章 2012-10-10
  • 打赏
  • 举报
回复
C#读取IE的Cache

http://www.web521.com/web/567968/T652930.shtml

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧