Cropping images in GDI +

Source: Internet
Author: User
Cutimage
1 void cutimage (httppostedfile post, string ppuid, out string imagename)
2 {
3 system. Drawing. Image sourceimg = system. Drawing. image. fromstream (post. inputstream );
4 If (sourceimg. Height> confighelper. userfacemaxheight)
5 {
6 This. _ lbl_upload_msg.text = "the maximum height cannot be greater than" + confighelper. userfacemaxheight;
7 return;
8}
9 If (sourceimg. width> confighelper. userfacemaxwidth)
10 {
11 This. _ lbl_upload_msg.text = "the maximum width cannot be greater than" + confighelper. userfacemaxwidth;
12 return;
13}
14 imageformat format = getimageformat (system. Io. Path. getextension (post. filename ));
15 string filename = ppuid + "." + format. tostring ();
16 imagename = filename;
17
18 if (! Userfacedir. endswith ("\\"))
19 userfacedir = userfacedir + "\\";
20 filename = userfacedir + filename;
21 int sourceimgwidth = sourceimg. width;
22 int sourceimgheight = sourceimg. height;
23 if (sourceimgwidth! = Confighelper. userfacewidth) & (sourceimgheight! = Confighelper. userfaceheight ))
24 {
25 // If the width and height ratio are, the thumbnail is directly formed.
26 if (double) sourceimgwidth/sourceimgheight) = 1)
27 {
28 system. Drawing. Image thumbimg = sourceimg. getthumbnailimage (confighelper. userfacewidth, confighelper. userfaceheight, null, intptr. Zero );
29 thumbimg. Save (filename, format );
30 thumbimg. Dispose ();
31 sourceimg. Dispose ();
32 return;
33}
34 bitmap bit = new Bitmap (sourceimg );
35 rectangle rec = new rectangle (); // construct a rectangle class and a rectangle
36 Rec. width = confighelper. userfacewidth;
37 Rec. Height = confighelper. userfaceheight;
38 If (sourceimgwidth> Rec. width)
39 Rec. x = (sourceimgwidth-Rec. width)/2;
40 else
41 {
42 Rec. x = 0;
43 Rec. width = sourceimg. width;
44}
45 if (sourceimgheight> Rec. Height)
46 Rec. Y = (sourceimgheight-Rec. Height)/2;
47 else
48 {
49 Rec. Y = 0;
50 Rec. Height = sourceimg. height;
51}
52
53 try
54 {
55 // here, we will clone the bitmap constructed during the upload process and crop it according to the defined rectangle.
56 bit. Clone (REC, pixelformat. dontcare). Save (filename, format );
57}
58 catch (exception ex)
59 {
60 this. _ lbl_upload_msg.text = ex. message;
61 return;
62}
63 finally
64 {
65 bit. Dispose ();
66 sourceimg. Dispose ();
67}
68}
69}
70

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.