C#文件相关操作
发布日期:2021-06-28 18:27:51
浏览次数:2
分类:技术文章
本文共 3263 字,大约阅读时间需要 10 分钟。
打开文件,并返回文件路径
public string OpenFile(string strFilter) { string strFileName = string.Empty; OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = strFilter; openFile.FilterIndex = 1; if (openFile.ShowDialog() == DialogResult.OK) { strFileName = openFile.FileName; } return strFileName; }
导出图片
public void ExportPic(PictureBox picBox) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "图像文件(*.PNG)|*.PNG|所有文件(*.*)|*.*"; if (saveFile.ShowDialog() == DialogResult.OK) { string fileName = saveFile.FileName; string strExtension = Path.GetExtension(fileName); switch (strExtension) { case ".png": picBox.Image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); break; case ".PNG": picBox.Image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); break; case ".jpg": picBox.Image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".jpeg": picBox.Image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break; default: break; } } }
将文件转换为二进制
public byte[] FileToBytes(string filePath) { FileInfo fileInfo = new FileInfo(filePath); byte[] buffer = new byte[fileInfo.Length]; FileStream fs = fileInfo.OpenRead(); fs.Read(buffer, 0, Convert.ToInt32(fileInfo.Length)); fs.Close(); return buffer; }
将给定二进制流写入一个新文件
public void CreateFile(byte[] fileBuffer,string newFilePath) { if (File.Exists(newFilePath)) { File.Delete(newFilePath); } FileStream fs = new FileStream(newFilePath, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); bw.Write(fileBuffer, 0, fileBuffer.Length); bw.Close(); fs.Close(); }
字节转图像
public Image Bytes2Img(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); return img; }
图片转字节
public byte[] Img2Bytes(Image img) { byte[] bytes = null; MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Png); bytes = ms.GetBuffer(); ms.Close(); return bytes; }
从图片文件路径转字节
public byte[] ImgFile2Bytes(string strPicFile) { if (string.IsNullOrEmpty(strPicFile)) return null; byte[] bytes = null; MemoryStream ms = new MemoryStream(); Image img = Image.FromFile(strPicFile); img.Save(ms, ImageFormat.Png); bytes = ms.GetBuffer(); ms.Close(); return bytes; }
转载地址:https://blog.csdn.net/xjjatdna/article/details/104490504 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月14日 07时41分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
原生安卓开发!驱动核心源码详解和Binder超系统学习资源,实战篇
2019-04-29
大牛手把手带你!2021年Android进阶者的新篇章,完整PDF
2019-04-29
太牛了!Android程序员最大的悲哀是什么?面试真题解析
2019-04-29
作为字节跳动面试官,BAT这种大厂履历意味着什么?赶快收藏备战金九银十!
2019-04-29
大牛深入讲解!Android面试中常问的MMAP到底是啥东东?再不刷题就晚了!
2019-04-29
太赞了!你会的还只有初级安卓工程师的技术吗?3面直接拿到offer
2019-04-29
深入解析Android-AutoLayout,全网疯传
2019-04-29
深入解析android核心组件和应用框架,最全Android知识总结
2019-04-29
深入解析android核心组件和应用框架,社招面试心得
2019-04-29
深度解析跳槽从开始到结束完整流程,持续更新中
2019-04-29
深度解析跳槽从开始到结束完整流程,面试真题解析
2019-04-29
hashmap扩容过程,字节大神强推千页PDF学习笔记,经典好文
2019-04-29
kotlin面试题!Android大厂高频面试题解析,薪资翻倍
2019-04-29
kotlin面试题!一口气拿了9家公司的offer,已拿offer入职
2019-04-29
retrofit优点,互联网寒冬公司倒闭后,年薪50W
2019-04-29
retrofit原理面试,Android性能优化最佳实践,面试必备
2019-04-29
【工作感悟】Android多进程从头讲到尾,offer拿到手软
2019-04-29
【微信小程序】面试一路绿灯Offer拿到手软,好文推荐
2019-04-29
Android之内存泄漏调试学习与总结,分享PDF高清版
2019-04-29
事件分发机制怎么回答,Android开发究竟该如何学习,使用指南
2019-04-29