防止圖片在WEB頁面上下載

今天應項目的要求做了個防止IE中圖片被下載的東東!
需說不是絕對的,但對於一般人來說來是可以的---個人認為:)
下面來看看實 現個程吧!

1. 前台界面:

 1<asp:DataList ID="dgClient" Runat="server" RepeatColumns="2">
 2    <ItemStyle Width="50%"></ItemStyle>
 3    <ItemTemplate>
 4        <table>
 5            <tr>
 6                <td>
 7                    <img src='./ShowImage.aspx?PATH=<%# Server.UrlEncode(DataBinder.Eval(Container.DataItem,"DTSmallImage").ToString())%>' border='0' width="133" height="100" />
 8                </td>
 9                <td class="intro0">
10                    <%#DataBinder.Eval(Container.DataItem,"DTTitle"%>
11                    <br>
12                    會員免費下載<br>
13                    >><href='<%#DataBinder.Eval(Container.DataItem,"DTBigImage") %>'>下載1024*768 </a>
14                    <br>
15                    >><href='<%#DataBinder.Eval(Container.DataItem,"DTSmallImage") %>'>下載800*600 </a>
16                    <br>
17                </td>
18            </tr>
19        </table>
20    </ItemTemplate>
21</asp:DataList>

其主要為以下這句代碼:
<img src='./ShowImage.aspx?PATH=<%# Server.UrlEncode(DataBinder.Eval(Container.DataItem,"DTSmallImage").ToString())%>' border='0' />
其上語句是通過ShowImage.aspx文件來緩存輸出圖 片,以達到顯示的只是圖片的影象效果,而不存在其實際圖片.所以在HTML代碼中看到的也只是<img src='./ShowImage.aspx?PATH=2005811184295.jpg' border='0'>,從而因無法獲得圖片路徑而不能下載,當然一般來說截頻可以獲得到對應的圖片,那麼下面看看
我是怎麼防止用戶截頻來 獲得正確的圖片的吧
2.ShowImage.aspx文件的後台代碼
 1string path = Request.QueryString["PATH"];
 2string EArtURL = ConfigurationSettings.AppSettings["EArtURL"];
 3path = EArtURL + "/NetWorkMamage1/DeskTopSmallImages/" +  path;
 4System.Net.WebClient client = new System.Net.WebClient();
 5string ExpFileName = FileLib.GetFileExName(path);
 6string LocalPath = Server.MapPath("./new111." + ExpFileName );
 7if(System.IO.File.Exists(LocalPath))
 8{
 9    FileLib.DeleteFile(LocalPath);
10}

11client.DownloadFile(path,LocalPath);
因以上圖片 都是放在其它WEB站台下面,所以采用以上簡單的代碼來保存到
當前站台下面的臨時文件中,圖上為當前目錄下的new111.jpg,因顯示的圖片 是不確定的個數也不確定,所以保存前先刪除掉其下原來的臨時文件,已保正
當前臨時文件為最新的文件
1System.IO.StreamReader reader = new System.IO.StreamReader(LocalPath);
2
3System.IO.Stream stream = reader.BaseStream;
4byte[] bys = new byte[stream.Length];
5stream.Read(bys,0,(int)stream.Length);
6System.IO.MemoryStream memory = new System.IO.MemoryStream(bys,0,(int)stream.Length);
7stream.Close();
如前台所示,因在一個頁面要顯示多個圖片,而臨時文件又 只有一個,如沒
有以上代碼那麼在下載第二個臨時文件時,定會因第一個文件正在使用而無
法保存,怎麼說:大家想想一般緩沖輸出圖片都是將這 個圖片所占的硬盤空
間讀入到內存中再輸出,因現在只存在一個臨時圖片文件,後來的文件確需
要刪除先前的文件以保持最新的操作(即 DataList每次邦定時都會調用ShowImage.aspx文件來顯示當前圖片),但當前文件雙在使用中需無法刪除或
保存,所以本人才采用 以上代碼來創建當前圖片文件的一個備份放入MemoryStream,再結束對實際圖片的調用stream.Close();
1ImagesEntry.ScaleImagesLib scanle = new ImagesEntry.ScaleImagesLib();
2                if(scanle.Create(memory,133,100))
3                {
4                    scanle.Show();
5                }

6                if(scanle.LastError!=null)
7                {
8                    //輸出出錯圖片
9                }

以上代碼是對圖片對進縮放處理
還可以對要顯示的圖片加上透明,水印,模糊處理等等,總之能讓其截頻出
也達不到實際的效果。

以 上是偶的一定愚見   

      還望大多多指正:) 

posted @ 2010-07-07 18:51  与时俱进  阅读(1009)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈