[WebApi] Tinkering with a resource manager--server-side split compressed image

Source: Internet
Author: User

Create a Web site or other Web application file Management interface (WEBAPI) fifth chapter "Server-side split compressed picture"

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: genius-android
Reprint Please specify source: http://blog.csdn.net/qiujuer/article/details/41826865
========================================================

History

[WebApi] Tinkering with a resource manager--database secondary server file access

In this

In the development of Web site, I often do some of the work: a large map contains a lot of small pictures, and the site in order to use one of the small picture I have to cut the big picture into a small picture. Although this is not a lot of work, but for me on the server side of the inexplicable storage of a lot of small files is very offensive, perhaps you will say using CSS to complete the large image of the small image display; Of course, it is possible. But what if I need to download the little picture? There's nothing you can do about it!

So there's a chapter of today's server-side cut-and-compress pictures.

Codetime Change


This version is relative to the previous one major two local changes: Add ImageUtils.cs, modify ResourceApiController.cs

ImageUtils.cs

Using system;using system.collections.generic;using system.drawing;using system.drawing.drawing2d;using System.drawing.imaging;using system.io;namespace webresource.models{public class Imageutils {//<summar        y>///ImageFormat//</summary>//<param name= "type" > Type </param>            <returns>ImageFormat</returns> public static ImageFormat GetFormat (String type) {            dictionary<string, imageformat> types = new dictionary<string, imageformat> (); Types.            ADD ("BMP", imageformat.bmp); Types.            ADD ("gif", imageformat.gif); Types.            ADD ("Ief", imageformat.jpeg); Types.            ADD ("JPEG", imageformat.jpeg); Types.            ADD ("JPG", imageformat.jpeg); Types.            ADD ("Jpe", imageformat.jpeg); Types.            ADD ("PNG", imageformat.png); Types.            ADD ("Tiff", Imageformat.tiff); Types.            ADD ("TIF", Imageformat.tiff); TypEs.            ADD ("DjVu", imageformat.bmp); Types.            ADD ("DJV", imageformat.bmp); Types.            ADD ("Wbmp", imageformat.bmp); Types.            ADD ("Ras", imageformat.bmp); Types.            ADD ("PNM", imageformat.bmp); Types.            ADD ("PBM", imageformat.bmp); Types.            ADD ("PGM", imageformat.bmp); Types.            Add ("ppm", imageformat.bmp); Types.            ADD ("RGB", imageformat.bmp); Types.            ADD ("XBM", imageformat.bmp); Types.            ADD ("xpm", imageformat.bmp); Types.            ADD ("xwd", imageformat.bmp); Types.            ADD ("ico", Imageformat.icon); Types.            ADD ("WMF", imageformat.emf); Types.            ADD ("Exif", imageformat.exif); Types.            ADD ("EMF", IMAGEFORMAT.EMF);                try {imageformat format = Types[type];            if (format! = NULL) return format;        } catch {} return imageformat.bmp;    }///<summary>//Picture converted to bytes    </summary>//<param name= "bitmap" > Pictures </param>//<param name= "type" > type <        /param>//<returns> bytecode </returns> public static byte[] Tobytes (Bitmap Bitmap, String type) {using (MemoryStream stream = new MemoryStream ()) {bitmap.                Save (Stream, GetFormat (type)); byte[] data = new Byte[stream.                Length]; Stream.                Seek (0, Seekorigin.begin); Stream. Read (data, 0, Convert.ToInt32 (stream).                Length));            return data; }}///<summary>//Adjust picture width height///</summary>//<param name= "b MP > Original bitmap </param>//<param name= "NEWW" > New width </param>//<param name= "NEWH" > New heights </param>//<returns> handling bitmap</returns> public static Bitmap Resize (Bitmap BM p, int neww, int newh) {TRy {Bitmap b = new Bitmap (NEWW, NEWH);                Graphics g = graphics.fromimage (b);                The quality g.interpolationmode of interpolation algorithm = Interpolationmode.default; G.drawimage (BMP, New Rectangle (0, 0, NEWW, NEWH), new Rectangle (0, 0, BMP. Width, BMP.                Height), GraphicsUnit.Pixel);                G.dispose ();            return b;            } catch {return null;        }}///<summary>///Slice bitmap: Specifies that the picture is split into row row, Col column, nth picture///</summary> <param name= "B" > Picture waiting to be split </param>//<param name= "Row" > Line </param>//<param NA Me= "col" > Columns </param>//<param name= "n" > Take nth pictures </param>//<returns> bitmap<             /returns> public static Bitmap Cut (Bitmap b, int row, int col, int n) {int w = B.width/col;   int h = b.height/row;         n = n > (col * row)?            Col * ROW:N;            int x = (n-1)% Col;            int y = (n-1)/col;            x = w * x;            y = h * y;                try {Bitmap bmpout = new Bitmap (W, H, Pixelformat.format24bpprgb);                Graphics g = graphics.fromimage (bmpout);                G.drawimage (b, New Rectangle (0, 0, W, h), New Rectangle (x, Y, W, h), graphicsunit.pixel);                G.dispose ();            return bmpout;            } catch {return null; }        }    }}
of this classthe effect is to compress and cut pictures, and its concrete implementation has been annotated clearly;If you do not understand the place, also look in the comments raised.

Then take a look at this API change.

ResourceApiController.cs

        <summary>//Get Image///</summary>//<param name= "name" >md5 name& lt;/param>//<returns>File</returns> [HttpGet] [Route ("{id}/img")] public asy            NC taskBecause of the large amount of code, I did not put it all out, the change of the place to write, which is mainly a newGetImageMethod.

In the method, we make certain judgments based on the parameters passed in and then cut the compressed image, and finally the package is returned in Http .

This is where the code changes.

Runtimeapi


You can see more than one interface in multiple APIs; go and see.


You can see that in addition to the Id is required, other parameters are the default values, you can not pass in.

    • Id: The MD5 value of the file for the interface to find the corresponding file
    • W: the width of the picture returned
    • H: The height of the picture returned
    • R:row row, which represents how many rows are split horizontally; default is 1
    • C:column column, which represents how many columns are split vertically; default is 1
    • N: Represents the nth picture after splitting

Upload


Convention, first upload a picture.

Access


Access address, you can use the original or can use the new address, the new address is simply added "/img" in the back

Compression


The above is the result of changing the width and height; You can right-click to save the picture to see if the picture is changed.

Shear

Take a look at the effect of shearing:


Talk about the principle:


Here's another one:


This is achieved, the server side of a picture; The client can be divided and compressed according to the requirements; of course, you can add some special effects, such as blur, Watermark and so on , which can be done by the service side.

End Resource

[WebApi] Tinkering with a resource manager--server-side split compressed image

Next Chapter

The next chapter prepares to combine these existing functions to make a management interface, so that the server can manage the data on the client side ; the thief is cool!

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
website: www.qiujuer.net
Open Source Library: genius-android
Reprint Please specify Source: http://blog.csdn.net/qiujuer/article/details/41826865
========================================================

[WebApi] Tinkering with a resource manager--server-side split compressed image

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.