From 0a70f99e51b0ea0c83288213e3b1f7904d78ede0 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 18 七月 2024 17:52:46 +0800 Subject: [PATCH] 创建PNG --- SimuTools/Tools/GdalHelper.cs | 74 +++++++++++++++++++++++++++++-------- 1 files changed, 58 insertions(+), 16 deletions(-) diff --git a/SimuTools/Tools/GdalHelper.cs b/SimuTools/Tools/GdalHelper.cs index 3da84f1..0a6e6ba 100644 --- a/SimuTools/Tools/GdalHelper.cs +++ b/SimuTools/Tools/GdalHelper.cs @@ -4,9 +4,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace SimuTools.Tools { @@ -83,9 +80,9 @@ Gdal.AllRegister(); } - /** - * 鑾峰彇Dataset鐨勬渶灏忕偣 - */ + /// <summary> + /// 鑾峰彇Dataset鐨勬渶灏忕偣 + /// </summary> public static Geometry GetMinPoint(Dataset ds) { double[] transform = new double[6]; @@ -100,9 +97,9 @@ return Transform(ds, point); } - /** - * 鑾峰彇Dataset鐨勬渶澶х偣 - */ + /// <summary> + /// 鑾峰彇Dataset鐨勬渶澶х偣 + /// </summary> public static Geometry GetMaxPoint(Dataset ds) { /* @@ -126,9 +123,9 @@ return Transform(ds, point); } - /** - * 鍧愭爣杞崲 - */ + /// <summary> + /// 鍧愭爣杞崲 + /// </summary> public static Geometry Transform(Dataset ds, Geometry point) { point.AssignSpatialReference(ds.GetSpatialRef()); @@ -151,9 +148,9 @@ return point; } - /** - * 鍒涘缓閲戝瓧濉� - */ + /// <summary> + /// 鍒涘缓閲戝瓧濉� + /// </summary> public static void CreatePyramid(String file) { Dataset ds = null; @@ -164,7 +161,7 @@ ds = Gdal.Open(file, Access.GA_ReadOnly); if (null == ds) return; - Band band = ds.GetRasterBand(1); + OSGeo.GDAL.Band band = ds.GetRasterBand(1); if (0 == band.GetOverviewCount()) { ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 }); @@ -179,5 +176,50 @@ if (null != ds) ds.Dispose(); } } + + /// <summary> + /// 鍒涘缓PNG + /// </summary> + public void CreatePng(string filePath, int width, int height, int bands = 3) + { + // 鍒涘缓鍐呭瓨椹卞姩 + OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM"); + // 鍒涘缓鍐呭瓨鏁版嵁闆� + Dataset ds = memDriver.Create("mem::", width, height, bands, DataType.GDT_Byte, null); + + // 璁剧疆鍥惧儚淇℃伅 + for (int i = 1; i <= bands; i++) + { + Band band = ds.GetRasterBand(i); + band.SetRasterColorInterpretation((ColorInterp)i); + } + + // 濉厖鍐呭瓨鍥惧儚 + byte[] buffer = new byte[width * height * bands]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)(i % 256); + } + + // 鍐欏叆鍐呭瓨鍥惧儚 + for (int i = 1; i <= bands; i++) + { + Band band = ds.GetRasterBand(i); + band.WriteRaster(0, 0, width, height, buffer, width, height, 0, 0); + } + + // 鍒涘缓PNG椹卞姩 + OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG"); + + // 淇濆瓨涓篜NG鏂囦欢 + //ds.SetProjection(Gdal.GetProjectionRef("")); + //ds.SetGeoTransform(Gdal.GetGeoTransform("")); + pngDriver.CreateCopy(filePath, ds, 0, null, null, null); + pngDriver.Dispose(); + + // 鍏抽棴鏁版嵁闆� + ds.Dispose(); + memDriver.Dispose(); + } } } -- Gitblit v1.9.3