• 图片操作以后删除提示文件被线程占用解决办法


    直接上代码

    public class ImageHelper
        {
            /// <summary>
            /// 图片转Base64编号
            /// </summary>
            /// <param name="imageFile"></param>
            /// <returns></returns>
            public static string ImageToBase64(string imageFile)
            {
                using (FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    //Bitmap bp = new Bitmap(imageFile);
                    //bp.Save(m, ImageFormat.Png);
                    //byte[] b = m.GetBuffer();
                    //string base64String = Convert.ToBase64String(b);
                    //m.Close();
                    //m.Dispose();
                    ////bp.Dispose();
                    //return base64String;
                    byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    MemoryStream ms = new MemoryStream(bytes);
                    Bitmap img = new Bitmap(ms);
                    img.Save(ms,ImageFormat.Png);
                    string base64String = Convert.ToBase64String(bytes);
                    return base64String;
                }
                
                
            }
    
            /// <summary>
            /// Base64编号转为图片
            /// </summary>
            /// <param name="base64String"></param>
            /// <param name="fileName"></param>
            /// <param name="format"></param>
            public static void Base64ToImage(string base64String, string fileName, ImageFormat format)
            {
                byte[] bt = Convert.FromBase64String(base64String);
                using (MemoryStream stream = new MemoryStream(bt))
                {
                    Bitmap bitmap = new Bitmap(stream);
                    bitmap.Save(fileName, format);
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
  • 相关阅读:
    SQL/存储过程和函数
    多个实现类 实现一个接口
    python 项目打包及分发
    HTML5 Canvas粒子文字特效
    算法思想之动态规划
    C#取消令牌CancellationTokenSource
    常见的Web攻击手段
    NET Core 迁移nuget包缓存到指定位置
    TCP长连接的优化
    C++容器不释放内存的解决办法
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/5911901.html
Copyright © 2020-2023  润新知