所谓的潇洒

导航

只读方式读取文件,防止被占用

  //显示图片

       pbCapture.Image = ExtendHelper.FileToImg(fileName); //pbCapture是System.Windows.Forms.PictureBox 

        public static Image FileToImg(string imageName)
        {
            try
            {

                using (FileStream s = new FileStream(imageName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var img = new Bitmap(s);
                    return img;
                }
            }
            catch (Exception ex)
            {
                LogHelper.logger.Error($"FileToImg({imageName})", ex);
                return null;
            }
        }

posted on 2020-04-26 11:30  所谓的潇洒  阅读(311)  评论(0编辑  收藏  举报