Esempio n. 1
0
        /// <summary>
        /// 将网络图片转化为Bitmap
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static (Bitmap, string) UrlToBitmap(string url)
        {
            // 随机生成图片名称
            var    fileName = Guid.NewGuid().ToString("N");
            Bitmap bitmap   = null;

            try
            {
                WebRequest request = WebRequest.Create(url);
                using (Stream stream = request.GetResponse().GetResponseStream())
                {
                    bitmap = new Bitmap(stream);
                    // 将png转化为jpg
                    if (bitmap.PixelFormat == PixelFormat.Format32bppArgb)
                    {
                        bitmap    = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
                        fileName += ".png";
                    }
                    else
                    {
                        fileName += ".jpg";
                    }
                    return(bitmap, fileName);
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
                fileName = "图片下载失败,检查[image_type]是否正确";
            }
            return(bitmap, fileName);
        }
Esempio n. 2
0
        public static List <Bitmap> SrcToBitmap(IEnumerable <string> imageSrcs)
        {
            List <Bitmap> bitmaps = new List <Bitmap>();

            foreach (var src in imageSrcs)
            {
                try
                {
                    string[] srcLinux = src.Replace("\\", "/").Split('/');

                    List <string> Srcc = new List <string>();

                    Srcc.Add(AppDomain.CurrentDomain.BaseDirectory);
                    Srcc.AddRange(srcLinux);
                    var      filePath = Path.Combine(Srcc.ToArray());
                    Bitmap   bitmap   = new Bitmap(filePath);
                    Bitmap   bm       = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
                    Graphics g        = Graphics.FromImage(bm);
                    g.DrawImageUnscaled(bitmap, 0, 0);
                    g.Dispose();
                    bitmaps.Add(bm);
                }
                catch (Exception ex)
                {
                    LogHelperNLog.Error(ex);
                }
            }
            return(bitmaps);
        }
Esempio n. 3
0
        /// <summary>
        /// 文件转化为图片(多个文件)
        /// </summary>
        /// <param name="files"></param>
        /// <returns>图片,base64,图片名</returns>
        public static List <(Bitmap, string, string)> FileToBitmap(List <IFormFile> files)
        {
            List <(Bitmap, string, string)> bitmaps = new List <(Bitmap, string, string)>();
            // 缓存路径
            var filePath = string.Format(@"{0}{1}/", AppDomain.CurrentDomain.BaseDirectory, "Temp");

            foreach (var file in files)
            {
                try
                {
                    if (file.Length > 0)
                    {
                        // 后缀名
                        var fileExtension = Path.GetExtension(file.FileName).ToLower();
                        // 自动创建文件夹
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        if (IMAGE_Extension.Contains(fileExtension))
                        {
                            using (var stream = file.OpenReadStream())
                            {
                                // 将文件转化为字节数组
                                byte[] arr = new byte[stream.Length];
                                stream.Read(arr, 0, (int)stream.Length);
                                //保存
                                //await file.CopyToAsync(stream);
                                Bitmap bitmap = new Bitmap(stream);
                                // 将png 转化为jpg,(PixelFormat.Format32bppRgb to PixelFormat.Format24bppRgb)
                                if (fileExtension == ".png")
                                {
                                    bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
                                }
                                // 转化为BASE64
                                var fileName = Guid.NewGuid().ToString("N") + fileExtension;

                                //var img = Base64ToBitmap(Convert.ToBase64String(arr));

                                bitmaps.Add((bitmap, Convert.ToBase64String(arr), fileName));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelperNLog.Error(ex, "FileCommon.FileToBitmapAsync 文件转换图片出错");
                }
            }
            return(bitmaps);
        }
Esempio n. 4
0
        /// <summary>
        /// Base64转图片
        /// </summary>
        /// <param name="base64str"></param>
        /// <returns>item1图片,item2图片名称</returns>
        public static (Bitmap, string) Base64ToBitmap(string base64str)
        {
            Bitmap bitmap = null;
            // 随机生成图片名称
            var fileName = Guid.NewGuid().ToString("N");
            // 去除头部
            var reg = new Regex("data:image/(.*);base64,");

            base64str = reg.Replace(base64str, "");

            try
            {
                // 将base64转化为字节
                var arr = Convert.FromBase64String(base64str);
                using (MemoryStream ms = new MemoryStream(arr))
                {
                    bitmap = new Bitmap(ms);
                    // 将png转化为jpg
                    if (bitmap.PixelFormat == PixelFormat.Format32bppArgb)
                    {
                        //bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
                        fileName += ".png";
                    }
                    else
                    {
                        fileName += ".jpg";
                    }

                    Bitmap   bm = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
                    Graphics g  = Graphics.FromImage(bm);
                    g.DrawImageUnscaled(bitmap, 0, 0);
                    g.Dispose();
                    bitmap = bm;
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
                bitmap   = null;
                fileName = "Base64图片不正确";
            }
            return(bitmap, fileName);
        }