c#調(diào)整圖片分辨率的實現(xiàn)示例
描述:項目上有個需求是每隔一段時間從redis拿圖片(圖片格式為base64)
拿到之后將圖片發(fā)送給led屏,這里嘗試了下,圖片拿取沒啥問題,發(fā)送給led屏也沒問題,但就是圖片沒顯示出來,后面查找后發(fā)現(xiàn),是因為圖片分辨率的原因。
所以才有了下面的操作:
//這里是拿取redis的數(shù)據(jù),并將他它轉(zhuǎn)成圖片 public void UpdateBaiduMap() { if (client.ContainsKey("map_theroad")) { try { string img = client.Get<string>("map_theroad"); img = img.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "").Replace("{", "").Replace("}", ""); ;//將base64頭部信息替換 byte[] bytes = Convert.FromBase64String(img); //本來準備 //直接將bytes[]發(fā)送給led的,后面發(fā)現(xiàn)不行 MemoryStream memStream = new MemoryStream(bytes); System.Drawing.Image mImage = System.Drawing.Image.FromStream(memStream); Bitmap bp = new Bitmap(mImage); string ImageFilePath = "../../LED/images"; if (System.IO.Directory.Exists("../../LED/images") == false)//如果不存在就創(chuàng)建文件夾 { System.IO.Directory.CreateDirectory(ImageFilePath); } string ImagePath = ImageFilePath + "/baidu_maps.png";//定義圖片名稱 //需要調(diào)整圖片分辨率 bp = KiResizeImage(bp, 170, 156); bp.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Png);//注意保存路徑 } catch (Exception e) { } } }
下面的方法是更改圖片分辨率
public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH) { try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的質(zhì)量 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch { return null; } }
到此這篇關(guān)于c#調(diào)整圖片分辨率的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)c# 圖片分辨率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法
這篇文章主要介紹了C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法,涉及C#基于iTextSharp操作pdf文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題 ,文中給大家列舉通過兩種方法來判斷,需要的朋友可以參考下2018-10-10WPF/Silverlight實現(xiàn)圖片局部放大的方法分析
這篇文章主要介紹了WPF/Silverlight實現(xiàn)圖片局部放大的方法,結(jié)合實例形式分析了WPF/Silverlight針對圖片屬性操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-03-03