Exemplo n.º 1
0
        public static string SaveFile(HttpPostedFileBase file)
        {
            string result = "";

            try
            {
                string directory = HttpRuntime.AppDomainAppPath.ToString();
                string path      = "/Upload";
                string extension = Path.GetExtension(file.FileName);

                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}", path, DateTime.Now.ToString("yyyyMMdd"));
                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}{2}{3}", path, DateTime.Now.ToString("yyyyMMddHHmmssfff"), new Random(GetRandomSeed()).Next(1, 1000), extension);
                file.SaveAs(directory + path.Replace("/", "\\"));

                if (File.Exists(directory + path.Replace("/", "\\")))
                {
                    result = path;
                }
            }
            catch (Exception ex)
            {
                Log4Helper.WriteLog("保存文件失败", ex);
            }

            return(result);
        }
Exemplo n.º 2
0
        public static bool DeleteFile(string path)
        {
            bool result = false;

            try
            {
                string directory = HttpRuntime.AppDomainAppPath.ToString();
                string url       = string.Format("{0}{1}", directory, path.Replace("/", "\\"));

                if (File.Exists(url))
                {
                    File.SetAttributes(url, FileAttributes.Normal);
                    File.Delete(url);
                }

                result = true;
            }
            catch (Exception ex)
            {
                Log4Helper.WriteLog("删除文件错误", ex);
            }
            Log4Helper.WriteLog("删除文件:" + path);

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据原图片和字节流二维码
        /// </summary>
        /// <param name="sourceImg"></param>
        /// <param name="topImg"></param>
        static public string CombinImageBitmap(string sourceImg, Bitmap topImg, string context)
        {
            string result = "";
            Image  img1   = Image.FromFile(Path.Combine(FileHelper.GetLocalPath(sourceImg))); //相框图片
            Image  img2   = Image.FromHbitmap(topImg.GetHbitmap());                           //照片图片

            try
            {
                FileHelper.CreatePath("/Generate");
                result = $"/Generate/{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.png";
                using (Graphics g = Graphics.FromImage(img1))
                {
                    g.DrawImage(img1, 0, 0, 374, 662); // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
                                                       //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
                                                       //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
                    if (!string.IsNullOrEmpty(context))
                    {
                        g.DrawString(context, new Font("微软雅黑", 18, FontStyle.Bold), Brushes.White, new Point(100, 615));
                    }

                    g.DrawImage(img2, 92, 374, 190, 193);
                    //g.DrawImage(img2, 310, 875, 470, 470);
                    GC.Collect();
                    img1.Save(FileHelper.GetLocalPath(result));
                    img1.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = "";
                Log4Helper.WriteLog("", ex);
            }
            finally
            {
                img1.Dispose();
                img2.Dispose();
            }

            return(result);
        }
Exemplo n.º 4
0
        public static string SaveFileForBase64(string file, string fileName)
        {
            string result = "";

            try
            {
                string directory = HttpRuntime.AppDomainAppPath.ToString();
                string path      = "/Upload";
                byte[] content   = Convert.FromBase64String(file.Substring(file.IndexOf("base64,") + 7));//切除前面那段image标识
                string extension = Path.GetExtension(fileName);

                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}", path, DateTime.Now.ToString("yyyyMMdd"));
                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}{2}{3}", path, DateTime.Now.ToString("yyyyMMddHHmmssfff"), new Random(GetRandomSeed()).Next(1, 1000), extension);

                using (MemoryStream ms = new MemoryStream(content))
                {
                    Bitmap bm = new Bitmap(ms);
                    bm.Save(directory + path.Replace("/", "\\"));
                }

                if (File.Exists(directory + path.Replace("/", "\\")))
                {
                    result = path;
                }
            }
            catch (Exception ex)
            {
                Log4Helper.WriteLog("保存文件失败", ex);
            }

            return(result);
        }