How to protect images in asp.net Website?


Hi

if we have to protect our images from being download or reusing by some other person on other website, we can protect our images using so many ways like

1.Disable right click using Javascript or Jquery
2.Keeping “WaterMark Text” on Image

How to disable right click using Javascript ?

This method is very easy to use, but if the user will disable the javascript then he can download the image.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Image_Protection_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>

<script language="javascript">

function right(e) {
var msg = "Sorry, you don’t have permission to download Image";

if (navigator.appName == ‘Netscape’ && e.which == 3) {
alert(msg);
return false;
}
if (navigator.appName == ‘Microsoft Internet Explorer’ && event.button==2) {
alert(msg);
return false;
}
else return true;
}

function trap()
{
if(document.images)
{
for(i=0;i<document.images.length;i++)
{
document.images[i].onmousedown = right;
document.images[i].onmouseup = right;
}
}
}

</script>

</head>
<body onLoad="trap()">
<form id="form1" runat="server">
<div>
<img src="../Img/Penguins.jpg" alt="" height="300px" height="200px" />
</div>
</form>
</body>
</html>

How to disable right click using Jquery ?

Here is the complete syntax

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Image_Protection_Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;
</script>
<script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
var msg = "Sorry, you don’t have permission to download Image";
alert(msg);
});
});

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<img src="../Img/Penguins.jpg" height="200Px" width="200px" alt="" />

</div>
</form>
</body>
</html>

For keeping watermar text on image read this article WaterMark Text on Image

Advertisement

2 thoughts on “How to protect images in asp.net Website?

  1. Xay September 17, 2011 / 6:38 pm

    Thank you for posted it saves my time and it helps a lot.

  2. Chandra Dev September 18, 2011 / 4:25 pm

    I am glad to know that my artical helped you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.