コード例 #1
0
ファイル: Program.cs プロジェクト: elainte/Glyphfriend
        private static IList <Glyph> ConvertGlyphsToList()
        {
            var glyphs         = new List <Glyph>();
            var glyphDirectory = new DirectoryInfo("../../Glyphs");

            foreach (var image in glyphDirectory.EnumerateFiles("*.png", SearchOption.AllDirectories))
            {
                var imageContent = StreamHelpers.ReadFully(new FileStream(image.FullName, FileMode.Open));
                var glyph        = new Glyph(Path.GetFileNameWithoutExtension(image.Name), image.Directory.Name, imageContent);

                glyphs.Add(glyph);
            }
            return(glyphs);
        }
コード例 #2
0
        public ActionResult UploadImg()
        {
            Stream imgStreamWithWaterMark = null;

            try
            {
                var file = System.Web.HttpContext.Current.Request.Files[0];

                Stream filestrem = file.InputStream; string ext = string.Empty;
                if (null != filestrem && file.ContentLength > 0)
                {
                    if (file.ContentLength > 1048576)
                    {
                        return(Json(new { Success = false, Msg = "上传失败(请按照规定大小上传)", Path = "" }, JsonRequestBehavior.AllowGet));
                    }
                    imgStreamWithWaterMark = filestrem;
                }// 上传的文件为空
                else
                {
                    log4net.LogHelper.WriteInfo(this.GetType(), "UploadImg fileStream为null!");
                    return(Json(new { Success = false, Msg = "上传图片失败", Path = "" }, JsonRequestBehavior.AllowGet));
                }
                imgStreamWithWaterMark.Position = 0;//保证流可读
                byte[] byteData = StreamHelpers.ReadFully(imgStreamWithWaterMark);
                Utility.ImgHelper.IsImgageType(byteData, "jpg", out ext);
                //不是图片格式不让上传
                string[] ImgType = new string[] { "jpg", "jpeg", "gif", "png", "bmp" };
                if (!ImgType.Contains <string>(ext.ToLower()))
                {
                    return(Json(new { Success = false, Msg = "不支持的格式,目前只支持jpg,jpeg,gif,png,bmp", Path = "" }, JsonRequestBehavior.AllowGet));
                }

                string aliTempImgKey = SaveImageToAliOSS(byteData);
                return(Json(new { Success = true, Msg = "上传图片成功", Path = aliTempImgKey }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                log4net.LogHelper.WriteError(GetType(), ex);
                return(Json(new { Success = false, Msg = "上传图片失败", Path = "" }, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                if (null != imgStreamWithWaterMark)
                {
                    imgStreamWithWaterMark.Dispose();
                }
            }
        }
コード例 #3
0
ファイル: uploadController.cs プロジェクト: soon14/vzan
        public ActionResult Image()
        {
            HttpFileCollection FileCollect = System.Web.HttpContext.Current.Request.Files;
            string             ext = string.Empty; Stream imgStreamWithWaterMark = null;
            int cityid = Context.GetRequestInt("cityid", 0);

            if (FileCollect.Count > 0)
            {
                try
                {
                    var    file      = FileCollect["upfile"];
                    Stream filestrem = file.InputStream;
                    if (null != filestrem)
                    {
                        //var text = string.Empty; var city = new C_CityInfoBLL().GetModel(cityid);
                        //if (null != city)
                        //{
                        //    text = city.CName;
                        //} //添加水印
                        //imgStreamWithWaterMark = StreamHelpers.AddTextWatermark(filestrem, text);
                        //if (null == imgStreamWithWaterMark)
                        imgStreamWithWaterMark = filestrem;//添加水印失败就不添加水印,继续保存
                    }// 上传的文件为空
                    else
                    {
                        log4net.LogHelper.WriteInfo(this.GetType(), "fileStream为null!");
                        return(Json(new { state = "error", msg = "fileStream null" }, "text/x-json"));
                    }
                    imgStreamWithWaterMark.Position = 0;//保证流可读
                    byte[] byteData = StreamHelpers.ReadFully(imgStreamWithWaterMark);
                    Utility.ImgHelper.IsImgageType(byteData, "jpg", out ext);
                    //不是图片格式不让上传
                    string[] ImgType = new string[] { "jpg", "jpeg", "gif", "png", "bmp" };
                    if (!ImgType.Contains <string>(ext.ToLower()))
                    {
                        return(Json(new { state = "error", msg = "不支持的格式" }, "text/x-json"));
                    }

                    if (null != byteData)
                    {
                        //同步到AliOss
                        string format = "." + ext;
                        string aliImgKey;
                        string aliTempImgFolder = AliOSSHelper.GetOssImgKey(ext, false, out aliImgKey);

                        bool putResult = AliOSSHelper.PutObjectFromByteArray(aliTempImgFolder, byteData, 1, format);
                        if (!putResult)
                        {
                            log4net.LogHelper.WriteInfo(this.GetType(), "图片同步到Ali失败!");
                        }

                        return(Json(new
                        {
                            originalName = file.FileName,
                            name = "",
                            url = aliImgKey,
                            size = file.ContentLength,
                            state = "SUCCESS",
                            type = format
                        }, "text/x-json"));
                    }
                    else
                    {
                        return(Json(new { state = "error", msg = "byteData null" }, "text/x-json"));
                    }
                }
                catch (Exception ex)
                {
                    log4net.LogHelper.WriteError(this.GetType(), ex);
                    return(Json(new { state = "error", msg = "runtime error" }, "text/x-json"));
                }
                finally
                {
                    if (null != imgStreamWithWaterMark)
                    {
                        imgStreamWithWaterMark.Dispose();
                    }
                }
            }
            else
            {
                return(Json(new { state = "error", msg = "no data to upload" }, "text/x-json"));
            }
        }
コード例 #4
0
ファイル: uploadController.cs プロジェクト: soon14/vzan
        public ActionResult UploadImg()
        {
            Stream imgStreamWithWaterMark = null;

            try
            {
                var    file           = System.Web.HttpContext.Current.Request.Files[0];
                int    needWarterMark = Context.GetRequestInt("awm", 0);
                int    cityid         = Context.GetRequestInt("cityid", 0);
                Stream filestrem      = file.InputStream;
                string ext            = string.Empty;
                if (null != filestrem && file.ContentLength > 0)
                {
                    //需要添加水印
                    //if (1 == needWarterMark)
                    //{
                    //    var text = string.Empty; var city = new C_CityInfoBLL().GetModel(cityid);
                    //    if (null != city)
                    //    {
                    //        text = city.CName;
                    //    } //添加水印
                    //    imgStreamWithWaterMark = StreamHelpers.AddTextWatermark(filestrem, text);
                    //    if (null == imgStreamWithWaterMark)
                    //        imgStreamWithWaterMark = filestrem;//添加水印失败就不添加水印,继续保存
                    //}
                    //else
                    //{
                    imgStreamWithWaterMark = filestrem;
                    //}
                }// 上传的文件为空
                else
                {
                    log4net.LogHelper.WriteInfo(this.GetType(), "UploadImg fileStream为null!");
                    return(Json(new { Success = false, Msg = "上传图片失败", Path = "" }, JsonRequestBehavior.AllowGet));
                }
                imgStreamWithWaterMark.Position = 0;//保证流可读
                byte[] byteData = StreamHelpers.ReadFully(imgStreamWithWaterMark);
                Utility.ImgHelper.IsImgageType(byteData, "jpg", out ext);
                //不是图片格式不让上传
                string[] ImgType = new string[] { "jpg", "jpeg", "gif", "png", "bmp" };
                if (!ImgType.Contains <string>(ext.ToLower()))
                {
                    return(Json(new { Success = false, Msg = "不支持的格式,目前只支持jpg,jpeg,gif,png,bmp", Path = "" }, JsonRequestBehavior.AllowGet));
                }

                string aliTempImgKey = SaveImageToAliOSS(byteData);
                return(Json(new { Success = true, Msg = "上传图片成功", Path = aliTempImgKey }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                log4net.LogHelper.WriteError(GetType(), ex);
                return(Json(new { Success = false, Msg = "上传图片失败", Path = "" }, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                if (null != imgStreamWithWaterMark)
                {
                    imgStreamWithWaterMark.Dispose();
                }
            }
        }