C#从IE缓存读取图片
如果用HttpWebRequest和HttpWebResponse从服务器取图片,会触发图片变化。于是想到从IE缓存读取图片
参考https://www.cnblogs.com/yelaiju/archive/2010/10/01/1839860.html和https://blog.csdn.net/annkie/article/details/6065521和http://www.mamicode.com/info-detail-370382.html
代码如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text; namespace Common
{
class Program
{//从IE缓存复制图片到D盘
static void Main(string[] args)
{
System.Diagnostics.Process.Start(" http://www.baidu.com ");
System.Threading.Thread.Sleep();
//打开IE缓存目录
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
IECache i = new IECache();
//在网页上找到百度logo图片的url写在下面
string b = i.GetPathForCachedFile("https://www.baidu.com/img/bd_logo1.png");
Console.WriteLine(b);
//从缓存中将bd_logo1.png拷贝到D盘下
File.Copy(b, @"d:\bd_logo1.png", true);
Console.WriteLine("现在打开D盘目录看看bd_logo1.png吧");
Console.ReadKey(); }
public class IECache
{
[DllImport("Wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean GetUrlCacheEntryInfo(String lpxaUrlName, IntPtr lpCacheEntryInfo, ref int lpdwCacheEntryInfoBufferSize);
const int ERROR_FILE_NOT_FOUND = 0x2;
struct LPINTERNET_CACHE_ENTRY_INFO
{
public int dwStructSize;
IntPtr lpszSourceUrlName;
public IntPtr lpszLocalFileName;
// int CacheEntryType;
// int dwUseCount;
// int dwHitRate;
//int dwSizeLow;
// int dwSizeHigh; //System.Runtime.InteropServices.ComTypes.FILETIME LastModifiedTime;
//System.Runtime.InteropServices.ComTypes.FILETIME Expiretime;
// System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
//System.Runtime.InteropServices.ComTypes.FILETIME LastSyncTime;
//IntPtr lpHeaderInfo;
//readonly int dwheaderInfoSize;
//IntPtr lpszFileExtension;
//int dwEemptDelta;
}
// 返回 指定URL文件的缓存在本地文件系统中的路径
public string GetPathForCachedFile(string fileUrl)
{
int cacheEntryInfoBufferSize = ;
IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
int lastError; Boolean result;
try
{
result = GetUrlCacheEntryInfo(fileUrl, IntPtr.Zero, ref cacheEntryInfoBufferSize);
lastError = Marshal.GetLastWin32Error();
if (result == false)
{
if (lastError == ERROR_FILE_NOT_FOUND)
return null;
}
cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
result = GetUrlCacheEntryInfo(fileUrl, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSize);
lastError = Marshal.GetLastWin32Error();
if (result == true)
{
Object strObj = Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(LPINTERNET_CACHE_ENTRY_INFO));
LPINTERNET_CACHE_ENTRY_INFO internetCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO)strObj;
String localFileName = Marshal.PtrToStringAuto(internetCacheEntry.lpszLocalFileName); return localFileName;
}
else
return null;// file not found
}
finally
{
if (!cacheEntryInfoBuffer.Equals(IntPtr.Zero))
Marshal.FreeHGlobal(cacheEntryInfoBuffer);
}
}
}
}
}
C#从IE缓存读取图片的更多相关文章
- 最蛋疼的bug:读取图片缩略图(一定要在相冊查看下形成缓存)
近期的一个连接服务端的应用.须要读取图片,一般供用户公布商品选择上传图片.初始的图片列表应该是缩略图.仅仅有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; imp ...
- SDWebImage缓存图片和读取图片
NSString *urlStr: NSUrl *url = [NSURL URLWithString:urlStr]; //缓存图片 SDWebImageManager *manager = [SD ...
- Cocos2d-x 在缓存创建图片
/* 加载图片资源到SpriteFrame缓存池*/ CCSpriteFrameCache *cache=CCSpriteFrameCache::sharedSpriteFrameCache( ...
- img src某个php文件输出图片(回复更改图片readfile读取图片等)
在论坛我们经常看到一回复图片就更改等,这功能是怎么实现的呢,其实更验证码道理相同. 新建文件 randimage.php 加入以下代码: <?php $dir='../../images/'; ...
- nodejs进阶(4)—读取图片到页面
我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...
- HTML中上传与读取图片或文件(input file)----在路上(25)
input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...
- nodeJS基础08:读取图片
1.读取图片 //server.js var http = require("http"); var readImage = require("./readImage&q ...
- opencv用imread( argv[1], 1)读取图片
显示一幅图:主要是运用功能:imread namedWindow imshowimread:从字面意思我们就可以看懂,用来读取图片的:namedWindow:显然,我们也可以看到这是用来命名窗口名称的 ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
随机推荐
- Centos6.5安装MySQL5.6备忘记录
Centos6.5安装MySQL5.6 1. 查看系统状态 [root@itzhouq32 tools]# cat /etc/issue CentOS release 6.5 (Final) Kern ...
- 判断浏览器的名称,区分360的ie和谷歌内核
function getBrowserInfo() { var ua = navigator.userAgent.toLocaleLowerCase(); var browserType = null ...
- js-new、object.create、bind的模拟实现【转载备忘】
//创建Person构造函数,参数为name,age function Person(name,age){ this.name = name; this.age = age; } function _ ...
- [新特性]PeopleTools8.54+:PeopleSoft Application Engine新特性
PeopleTools 8.54 的Application Engine 已经被更新,特别是在AE跟踪设置中有了更多的选项,本文将帮助您了解8.54的新AE特性以及如何使用这些特性. AE trace ...
- Dynamics AX 2012 性能优化之 SQL Server 复制
Dynamics AX 2012 性能优化之 SQL Server 复制 分析数据滞后 在博文 Dynamics AX 2012 在BI分析中建立数据仓库的必要性 里,Reinhard 阐述了在 AX ...
- ThreadLocal 类 的源码解析以及使用原理
1.原理图说明 首先看这一张图,我们可以看出,每一个Thread类中都存在一个属性 ThreadLocalMap 成员,该成员是一个map数据结构,map中是一个Entry的数组,存在entry实体, ...
- android常犯错误记录(一)
错误:Error:Error: Found item Attr/border_width more than one time 这个容易,属性相同了,按照提示查询一下找出来删了就行了,注意大小写很容易 ...
- 在a标签内添加hover样式的方法:
<a href="javascript:void(0);" onmouseover="this.style.color='yellow';" onmous ...
- Android性能优化9-ANR完全解析
1.什么是ANR 在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对 ...
- Linux 源码阅读 进程管理
Linux 源码阅读 进程管理 版本:2.6.24 1.准备知识 1.1 Linux系统中,进程是最小的调度单位: 1.2 PCB数据结构:task_struct (Location:linux-2. ...