MVC generate images, download files (images do not exist locally, download on-line)

Source: Internet
Author: User

//<summary>
//Generate picture
//</summary>
//<param Name= "collection" ></PARAM>
<returns></returns>
Public Filestreamresult generateimage (formcollection collection)
{
var shopid = Shopcaches.getshopinfo ();
var headimginfo = Getresourcepath (Shopid.shopid, collection["Dayinpersonnelid"], "Headfront");
var imageinfo = Getresourcepath (Shopid.shopid, collection["Inputgonghao"], "Personnobarcode");//turn to Picture type
var FilePath = Server.MapPath (string. Format ("~/ui/images/workcardpicture/{0}", Shopid.shopid));
if (! Directory.Exists (FilePath))
Directory.CreateDirectory (filePath);
Const int WIDTH = HEIGHT = n;
var fon t12b = new Font (Fontfamily.genericserif, 12.0f, fontstyle.regular);
var font20b = new Font (Fontfamily.genericserif, 15.0f, fontstyle.bold);

using (var bitmap = new Bitmap (WIDTH, HEIGHT))
{
using (Graphics garphics = graphics.fromimage (bitmap))
{ br> Garphics. Clear (Color.White);
Garphics. DrawString (Shopid.shopname, font20b, Brushes.black, 126, 10);
if (headimginfo! = null)
{
Garphics. DrawImage (Headimginfo, 38, 57, 100, 110); Photo
}
Garphics. DrawString ("Work number:" + collection["Inputgonghao"], font12b, Brushes.black, 197, 53);
Garphics. DrawString ("Name:" + collection["inputxingming"], font12b, Brushes.black, 197, 78);
Garphics. DrawString ("Position:" + collection["Inputzhiwei"], font12b, Brushes.black, 197, 103);
Garphics. DrawImage (Imageinfo, New Point (197, 151)); Barcode

Bitmap. Save (FilePath + "/" + collection["Inputgonghao"] + ". jpg");
Imageinfo.dispose ();//Release all the resources for the barcode
Garphics. Dispose ();//Release resources
Bitmap. Dispose ();//Release resources
}
}
var path = FilePath + "\ \" + collection["Inputgonghao"] + ". jpg";
Return File (New FileStream (Path, FileMode.Open), "Image/jpeg", Server.URLEncode (collection["Inputgonghao"] + ". jpg") ;
Return Json ("");
}

<summary>
Generate Picture: Get the path to the picture (local only; local no resource server)
</summary>
<param name= "Shopid" > Shop id</param>
<param name= "FileName" > The name of the image to be taken </param>
<param name= "FolderName" > folder name </param>
<returns> Picture Path </returns>
Private Image Getresourcepath (Guid shopid, String fileName, String folderName)
{
Image Headimginfo;
Const string URL = "~/ui/images";
string httpurl = Webconfig.resourceserverinformation;
Determine if there is a local
var path = Server.MapPath (string. Format ("{0}/{1}/{2}/{3}", URL, FolderName, shopid, FileName + ". jpg"));
var Httppath = string. Format ("{0}/{1}/{2}/{3}", Httpurl, FolderName, shopid, FileName + ". jpg");
if (System.IO.File.Exists (path))
{
Headimginfo = image.fromfile (path);
}
Else
{
var FilePath = Server.MapPath (string. Format ("{0}/{1}/{2}", URL, FolderName, shopid));
Local no, first download the picture down in the conversion
if (! Directory.Exists (FilePath))
Directory.CreateDirectory (FilePath);
String Dizhi = Server.MapPath (string. Format ("{0}/{1}/{2}/{3}", URL, FolderName, shopid, FileName + ". jpg"));
var webClient = new WebClient ();
Try
{
Webclient.downloadfile (Httppath, Dizhi);//download pictures on the network, save in Local
Headimginfo = Image.FromFile (Dizhi);
}
Catch
{
Headimginfo = null;
}
}
return headimginfo;
}

MVC generate images, download files (images do not exist locally, download on-line)

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.