public void Save(int xoff, int yoff, int buf_xsize, int buf_ysize, double[] buffer) { try { OSGeo.GDAL.Gdal.AllRegister(); _dataSet = OSGeo.GDAL.Gdal.Open(_fileName, OSGeo.GDAL.Access.GA_Update); _band1 = _dataSet.GetRasterBand(1); CPLErr err1 = _band1.WriteRaster(xoff, yoff, _resultInfo.Width, buf_ysize, buffer, buf_xsize, buf_ysize, 0, 0); _dataSet.Dispose(); _band1.Dispose(); } catch (Exception ex) { throw new Exception("数据写入失败。" + ex.Message); } }
/// <summary> /// 数据保存 /// </summary> public void Save() { try { OSGeo.GDAL.Gdal.AllRegister(); _dataSet = OSGeo.GDAL.Gdal.Open(_fileName, OSGeo.GDAL.Access.GA_Update); _band1 = _dataSet.GetRasterBand(1); CPLErr err1 = _band1.WriteRaster(0, 0, _resultInfo.Width, _resultInfo.Height, _pixels1, _resultInfo.Width, _resultInfo.Height, 0, 0); _dataSet.Dispose(); _band1.Dispose(); } catch (Exception ex) { throw new Exception("数据写入失败。" + ex.Message); } }
private static void WriteGrayIndex(Bitmap value, int xOffset, int yOffset, Band first) { int width = value.Width; int height = value.Height; BitmapData bData = value.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int stride = Math.Abs(bData.Stride); byte[] r = new byte[stride * height * 4]; Marshal.Copy(bData.Scan0, r, 0, r.Length); byte[] vals = new byte[height * stride]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { byte blue = r[row * stride + col * 4]; byte green = r[row * stride + col * 4 + 1]; byte red = r[row * stride + col * 4 + 2]; int gray = Convert.ToInt32(.3 * red + .59 * green + .11 * blue); if (gray > 255) gray = 255; if (gray < 0) gray = 0; vals[row * width + col] = Convert.ToByte(gray); } } first.WriteRaster(xOffset, yOffset, width, height, vals, width, height, 0, 0); }
private static void WriteRgb(Bitmap value, int xOffset, int yOffset, Band first, Dataset set) { if (set.RasterCount < 3) { throw new GdalException( "RGB Format was indicated but there are only " + set.RasterCount + " bands!"); } int width = value.Width; int height = value.Height; BitmapData bData = value.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = Math.Abs(bData.Stride); byte[] vals = new byte[stride * height]; Marshal.Copy(bData.Scan0, vals, 0, vals.Length); value.UnlockBits(bData); byte[] r = new byte[width * height]; byte[] g = new byte[width * height]; byte[] b = new byte[width * height]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { b[row * width + col] = vals[row * stride + col * 4]; g[row * width + col] = vals[row * stride + col * 4 + 1]; r[row * width + col] = vals[row * stride + col * 4 + 2]; } } Band green = set.GetRasterBand(2); Band blue = set.GetRasterBand(3); first.WriteRaster(xOffset, yOffset, width, height, r, width, height, 0, 0); first.FlushCache(); green.WriteRaster(xOffset, yOffset, width, height, g, width, height, 0, 0); green.FlushCache(); blue.WriteRaster(xOffset, yOffset, width, height, b, width, height, 0, 0); blue.FlushCache(); // first disposed in caller green.Dispose(); blue.Dispose(); }
private static void WritePaletteBuffered(Bitmap value, int xOffset, int yOffset, Band first) { ColorTable ct = first.GetRasterColorTable(); if (ct == null) { throw new GdalException("Image was stored with a palette interpretation but has no color table."); } if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB) { throw new GdalException("Only RGB palette interpretation is currently supported by this " + " plug-in, " + ct.GetPaletteInterpretation() + " is not supported."); } int width = value.Width; int height = value.Height; BitmapData bData = value.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int stride = Math.Abs(bData.Stride); byte[] r = new byte[stride * height]; Marshal.Copy(bData.Scan0, r, 0, r.Length); value.UnlockBits(bData); byte[] vals = new byte[width * height]; byte[][] colorTable = new byte[ct.GetCount()][]; for (int i = 0; i < ct.GetCount(); i++) { ColorEntry ce = ct.GetColorEntry(i); colorTable[i] = new[] { (byte) ce.c3, (byte) ce.c2, (byte) ce.c1, (byte) ce.c4 }; } for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { vals[row * width + col] = MatchColor(r, row * stride + col * 4, colorTable); } } first.WriteRaster(xOffset, yOffset, width, height, vals, width, height, 0, 0); }