Asp.net implements online screenshots!

Source: Internet
Author: User

Transfer music beer
Post

 

Recently, I have read that some websites provide excellent online functions. After uploading an image, you can crop it to a fixed size for your own photo.
I this article is to achieve this function, the development tool is vs2005 and two JS class libraries, dom-drag.js and jquery, the use of relatively simple, the two JS class libraries are hate excellent, I suggest you look at it again.
First let's take a look


This effect is simulated, so we won't talk about how to upload images or other small issues. The large image has been uploaded to the server. Then there is a black border with a frame length and a width of PX. We just want to intercept this
The size of the image is used as our avatar. You can drag the black box to a proper position to capture the desired image. In the IE7 effect, you must click the border to drag it. In Firefox, you can click any of the black boxes.
Location. In addition, I have no boundary limit for this black box, that is, he should not drag out the range of this large image. An error is returned if it is dragged out.
We need to create the cutimage. aspx and cutimageajax. aspx pages.
Cutimage. aspx is what we see.
<Script language = JavaScript src = drag. js> </SCRIPT>
<Script language = JavaScript src = jquery-1.2.3.pack.js> </SCRIPT>
Add two JS Class Libraries
Then the HTML tag in the body. The following figure shows the HTML format (aa.gif is the big image we use for testing)
<Div style = "position: relative;">


<Div style = "border: solid 2px black; width: 100px; Height: 100px;
Cursor: hand; position: absolute; left: 0px; Z-index: 10; top: 0px;
Background-color: Black filter: alpha (opacity = 20 );"
Id = "divcut"> </div>

</Div>
<HR/>
<Input type = button onclick = "cut ()" value = "Confirm crop"/>
<HR/>
Cropped results:

<HR/>
<Div style = "border: solid 1px black;" id = "divrecord"> </div>

Then there is the JS Code.
<Script language = JavaScript>
Function Init () // initialization execution
{
VaR cutdiv = Document. getelementbyid ('divcut ');
Drag. INIT (cutdiv); // This is the key to dragging to the Black Box.
Cutdiv. ondrag = function (x, y) // This is the event triggered when the black box is dragged. X and Y are the coordinates.
{

Document. getelementbyid ("divrecord"). innerhtml = "coordinates of cutdiv" + x + '--'
+ Y + "<br> image coordinate" + document. getelementbyid ("im"). offsetleft +
"-" + Document. getelementbyid ("im"). offsettop;
}
}
Function cut () // cropping button event
{
VaR x = Document. getelementbyid ("divcut"). offsetleft-10; // minus 10 is because the starting position of the big image X is 10.
Var y = Document. getelementbyid ("divcut"). offsettop;


$. Get ("cutimageajax. aspx? T = "+ (new date (). valueof (), {offx: x
, Offy: y}, callback); // Ajax method in jquery, which is relatively simple. The coordinates of X and Y are passed in.
}
Function callback (RES) // This is the callback function. If RES is equal to null, the cropped image is displayed successfully. The address and name of the cropped image are fixed.
{
If (RES = '')
{
Document. getelementbyid ('imgresresult'). src = 'pic/aa1.gif ';
}
}
</SCRIPT>


Then you should check the background processing of the cutimageajax. ASPX page. In fact, it is relatively simple to crop images in the background.
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
String x = page. Request. querystring ["offx"]. tostring ();
String y = page. Request. querystring ["offy"]. tostring ();

Bitmap Bm = new Bitmap (server. mappath ("PIC/aa.gif "));
Rectangle clonerect = new rectangle (Int. parse (x), Int. parse (Y), 100,100 );
Pixelformat format = BM. pixelformat;
Bitmap clonebitmap = BM. Clone (clonerect, format );

// === Save the image ====
Clonebitmap. Save (server. mappath ("PIC/aa1.gif"), imageformat. GIF );
}
}
Check it out. The key cropping code is like this. However, I have not made any other judgment here, such as the slice format. If I want to upload images of different formats, I will not explain it here, but the truth is almost the same.

This is over. O (partition _ partition) O... haha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.