c#調整圖片分辨率的實現示例
更新時間:2023年10月31日 14:43:21 作者:墨水直達
本文主要介紹了c#調整圖片分辨率的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
描述:項目上有個需求是每隔一段時間從redis拿圖片(圖片格式為base64)
拿到之后將圖片發(fā)送給led屏,這里嘗試了下,圖片拿取沒啥問題,發(fā)送給led屏也沒問題,但就是圖片沒顯示出來,后面查找后發(fā)現,是因為圖片分辨率的原因。
所以才有了下面的操作:
//這里是拿取redis的數據,并將他它轉成圖片 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ā)現不行 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";//定義圖片名稱 //需要調整圖片分辨率 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); // 插值算法的質量 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; } }
到此這篇關于c#調整圖片分辨率的實現示例的文章就介紹到這了,更多相關c# 圖片分辨率內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題 ,文中給大家列舉通過兩種方法來判斷,需要的朋友可以參考下2018-10-10