本文整理匯總了C#中OSGeo.GDAL.Dataset.GetRasterBand方法的典型用法代碼示例。如果您正苦於以下問題:C# Dataset.GetRasterBand方法的具體用法?C# Dataset.GetRasterBand怎麽用?C# Dataset.GetRasterBand使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類OSGeo.GDAL.Dataset
的用法示例。
在下文中一共展示了Dataset.GetRasterBand方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GDAL_Info
public GDAL_Info(Dataset ds)
{
Projection = ds.GetProjectionRef();
Resolution = new Size(ds.RasterXSize, ds.RasterYSize);
Bands = new DataBandInfo[ds.RasterCount];
for (int i = 0; i < ds.RasterCount; i++)
{
Band band = ds.GetRasterBand(i + 1);
Bands[i] = new DataBandInfo();
int temp;
band.GetMaximum(out Bands[i].MaxValue, out temp);
band.GetMaximum(out Bands[i].MinValue, out temp);
band.GetNoDataValue(out Bands[i].NODATAValue, out temp);
ColorInterp clr = band.GetRasterColorInterpretation();
switch (clr)
{
case ColorInterp.GCI_RedBand:
Bands[i].Name = "RedBand";
Bands[i].Image = Resource1.red_layer;
BppType += "R";
break;
case ColorInterp.GCI_GreenBand:
Bands[i].Name = "GreenBand";
Bands[i].Image = Resource1.green_layer;
BppType += "G";
break;
case ColorInterp.GCI_BlueBand:
Bands[i].Name = "BlueBand";
Bands[i].Image = Resource1.blue_layer;
BppType += "B";
break;
default:
Bands[i].Name = clr.ToString();
Bands[i].Image = null;
BppType += "?";
break;
}
BppType += "[" + Gdal.GetDataTypeName(band.DataType) + "]";
Bpp += (ushort)Gdal.GetDataTypeSize(band.DataType);
if (i + 1 < ds.RasterCount)
BppType += ",";
}
BppType += " (" + Bpp + ")";
Driver = ds.GetDriver().LongName;
Metadata = ds.GetMetadata("");
ImgMetadata = ds.GetMetadata("IMAGE_STRUCTURE");
SubDSMetadata = ds.GetMetadata("SUBDATASETS");
GeoLocMetadata = ds.GetMetadata("GEOLOCATION");
GDALInfoGetPosition(ds, 0.0, 0.0, out UpperLeftX, out UpperLeftY);
GDALInfoGetPosition(ds, 0.0, ds.RasterYSize, out UpperRightX, out UpperRightY);
GDALInfoGetPosition(ds, ds.RasterXSize, 0.0, out LowerLeftX, out LowerLeftY);
GDALInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize, out LowerRightX, out LowerRightY);
GDALInfoGetPosition(ds, ds.RasterXSize / 2, ds.RasterYSize / 2, out CenterX, out CenterY);
}
示例2: GdalImage
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="ds"></param>
/// <param name="band"></param>
internal GdalImage(string filename, Dataset ds, ImageBandType band)
{
_dataset = ds;
if (band == ImageBandType.ARGB)
{
using (Band bnd = ds.GetRasterBand(1))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_AlphaBand);
}
using (Band bnd = ds.GetRasterBand(2))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_RedBand);
}
using (Band bnd = ds.GetRasterBand(3))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_GreenBand);
}
using (Band bnd = ds.GetRasterBand(4))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_BlueBand);
}
}
else if (band == ImageBandType.RGB)
{
using (Band bnd = ds.GetRasterBand(1))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_RedBand);
}
using (Band bnd = ds.GetRasterBand(2))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_GreenBand);
}
using (Band bnd = ds.GetRasterBand(3))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_BlueBand);
}
}
else if (band == ImageBandType.PalletCoded)
{
using (Band bnd = ds.GetRasterBand(3))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_PaletteIndex);
}
}
else
{
using (Band bnd = ds.GetRasterBand(3))
{
bnd.SetRasterColorInterpretation(ColorInterp.GCI_GrayIndex);
}
}
Filename = filename;
WorldFile = new WorldFile { Affine = new double[6] };
Gdal.AllRegister();
}
示例3: Open
/// <summary>
/// Attempts to open the specified file.
/// </summary>
public override void Open()
{
_dataset = Helpers.Open(Filename);
_red = _dataset.GetRasterBand(1);
if (_red.GetRasterColorInterpretation() == ColorInterp.GCI_PaletteIndex)
{
ReadPaletteBuffered();
}
if (_red.GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex)
{
ReadGrayIndex();
}
if (_red.GetRasterColorInterpretation() == ColorInterp.GCI_RedBand)
{
ReadRgb();
}
if (_red.GetRasterColorInterpretation() == ColorInterp.GCI_AlphaBand)
{
ReadArgb();
}
}
示例4: GetNonRotatedPreview
//.........這裏部分代碼省略.........
else
dblLocY = size.Height - dblImginMapH;
}
bitScalar = GetBitScalar();
try
{
bitmap = new Bitmap((int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH),
PixelFormat.Format24bppRgb);
bitmapData =
bitmap.LockBits(
new Rectangle(0, 0, (int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH)),
ImageLockMode.ReadWrite, bitmap.PixelFormat);
byte cr = _noDataInitColor.R;
byte cg = _noDataInitColor.G;
byte cb = _noDataInitColor.B;
//
Double[] noDataValues = new Double[Bands];
Double[] scales = new Double[Bands];
ColorTable colorTable = null;
unsafe
{
double[][] buffer = new double[Bands][];
Band[] band = new Band[Bands];
int[] ch = new int[Bands];
// get data from image
for (int i = 0; i < Bands; i++)
{
buffer[i] = new double[(int)Math.Round(dblImginMapW) * (int)Math.Round(dblImginMapH)];
band[i] = dataset.GetRasterBand(i + 1);
//get nodata value if present
Int32 hasVal = 0;
band[i].GetNoDataValue(out noDataValues[i], out hasVal);
if (hasVal == 0) noDataValues[i] = Double.NaN;
band[i].GetScale(out scales[i], out hasVal);
if (hasVal == 0) scales[i] = 1.0;
band[i].ReadRaster((int)Math.Round(x1), (int)Math.Round(y1), (int)Math.Round(imgPixWidth),
(int)Math.Round(imgPixHeight),
buffer[i], (int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH),
0, 0);
switch (band[i].GetRasterColorInterpretation())
{
case ColorInterp.GCI_BlueBand:
ch[i] = 0;
break;
case ColorInterp.GCI_GreenBand:
ch[i] = 1;
break;
case ColorInterp.GCI_RedBand:
ch[i] = 2;
break;
case ColorInterp.GCI_Undefined:
if (Bands > 1)
ch[i] = 3; // infrared
else
{
ch[i] = 4;
if (_colorBlend == null)
{
示例5: WriteRgb
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();
}
示例6: GetColorPalette
/// <summary>
/// This is only used in the palette indexed band type.
/// </summary>
public override IEnumerable<Color> GetColorPalette()
{
if (ColorPalette == null)
{
try
{
_dataset = Gdal.Open(Filename, Access.GA_Update);
}
catch
{
try
{
_dataset = Gdal.Open(Filename, Access.GA_ReadOnly);
}
catch (Exception ex)
{
throw new GdalException(ex.ToString());
}
}
Band first = _dataset.GetRasterBand(1);
ColorTable ct = first.GetRasterColorTable();
int count = ct.GetCount();
List<Color> result = new List<Color>();
for (int i = 0; i < count; i++)
{
ColorEntry ce = ct.GetColorEntry(i);
result.Add(Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3));
}
ColorPalette = result;
}
return ColorPalette;
}
示例7: GetNonRotatedPreview
//.........這裏部分代碼省略.........
// ratios of bounding box to image ground space
double dblBBoxtoImgX = (double)size.Width / dblBBoxW;
double dblBBoxtoImgY = (double)size.Height / dblBBoxH;
// set where to display bitmap in Map
if (bbox.Left != left)
{
if (bbox.Right != right)
dblLocX = (_Envelope.Left - bbox.Left) * dblBBoxtoImgX;
else
dblLocX = (double)size.Width - dblImginMapW;
}
if (bbox.Top != top)
{
if (bbox.Bottom != bottom)
dblLocY = (bbox.Top - _Envelope.Top) * dblBBoxtoImgY;
else
dblLocY = (double)size.Height - dblImginMapH;
}
// scale
if (_bitDepth == 12)
bitScalar = 16.0;
else if (_bitDepth == 16)
bitScalar = 256.0;
else if (_bitDepth == 32)
bitScalar = 16777216.0;
try
{
bitmap = new Bitmap((int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH), PixelFormat.Format24bppRgb);
bitmapData = bitmap.LockBits(new Rectangle(0, 0, (int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH)), ImageLockMode.ReadWrite, bitmap.PixelFormat);
unsafe
{
double[][] buffer = new double[Bands][];
Band[] band = new Band[Bands];
int[] ch = new int[Bands];
// get data from image
for (int i = 0; i < Bands; i++)
{
buffer[i] = new double[(int)Math.Round(dblImginMapW) * (int)Math.Round(dblImginMapH)];
band[i] = dataset.GetRasterBand(i + 1);
band[i].ReadRaster((int)Math.Round(x1), (int)Math.Round(y1), (int)Math.Round(imgPixWidth), (int)Math.Round(imgPixHeight),
buffer[i], (int)Math.Round(dblImginMapW), (int)Math.Round(dblImginMapH), 0, 0);
if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_BlueBand) ch[i] = 0;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_GreenBand) ch[i] = 1;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_RedBand) ch[i] = 2;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_Undefined) ch[i] = 3; // infrared
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex) ch[i] = 4;
else ch[i] = -1;
}
if (_bitDepth == 32)
ch = new int[] { 0, 1, 2 };
p_indx = 0;
for (int y = 0; y < Math.Round(dblImginMapH); y++)
{
byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
for (int x = 0; x < Math.Round(dblImginMapW); x++, p_indx++)
{
for (int i = 0; i < Bands; i++)
{
intVal[i] = buffer[i][p_indx] / bitScalar;
if (_colorCorrect)
{
intVal[i] = ApplyColorCorrection(intVal[i], 0, ch[i], 0, 0);
if (_lbands >= 3)
_histogram[_lbands][(int)(intVal[2] * 0.2126 + intVal[1] * 0.7152 + intVal[0] * 0.0722)]++;
}
if (intVal[i] > 255)
intVal[i] = 255;
}
WritePixel(x, intVal, iPixelSize, ch, row);
}
}
}
}
catch
{
return;
}
finally
{
if (bitmapData != null)
bitmap.UnlockBits(bitmapData);
}
}
if (_transparentColor != Color.Empty)
bitmap.MakeTransparent(_transparentColor);
g.DrawImage(bitmap, new System.Drawing.Point((int)Math.Round(dblLocX), (int)Math.Round(dblLocY)));
}
示例8: SaveBitmapDirect
private static void SaveBitmapDirect(string filename, Dataset ds, int xOff, int yOff, int width, int height, int imageWidth, int imageHeight)
{
if (ds.RasterCount == 0)
return;
int[] bandMap = new int[4] { 1, 1, 1, 1 };
int channelCount = 1;
bool hasAlpha = false;
bool isIndexed = false;
int channelSize = 8;
ColorTable ct = null;
// Evaluate the bands and find out a proper image transfer format
for (int i = 0; i < ds.RasterCount; i++)
{
Band band = ds.GetRasterBand(i + 1);
if (Gdal.GetDataTypeSize(band.DataType) > 8)
channelSize = 16;
switch (band.GetRasterColorInterpretation())
{
case ColorInterp.GCI_AlphaBand:
channelCount = 4;
hasAlpha = true;
bandMap[3] = i + 1;
break;
case ColorInterp.GCI_BlueBand:
if (channelCount < 3)
channelCount = 3;
bandMap[0] = i + 1;
break;
case ColorInterp.GCI_RedBand:
if (channelCount < 3)
channelCount = 3;
bandMap[2] = i + 1;
break;
case ColorInterp.GCI_GreenBand:
if (channelCount < 3)
channelCount = 3;
bandMap[1] = i + 1;
break;
case ColorInterp.GCI_PaletteIndex:
ct = band.GetRasterColorTable();
isIndexed = true;
bandMap[0] = i + 1;
break;
case ColorInterp.GCI_GrayIndex:
isIndexed = true;
bandMap[0] = i + 1;
break;
default:
// we create the bandmap using the dataset ordering by default
if (i < 4 && bandMap[i] == 0)
{
if (channelCount < i)
channelCount = i;
bandMap[i] = i + 1;
}
break;
}
}
// find out the pixel format based on the gathered information
PixelFormat pixelFormat;
DataType dataType;
int pixelSpace;
if (isIndexed)
{
pixelFormat = PixelFormat.Format8bppIndexed;
dataType = DataType.GDT_Byte;
pixelSpace = 1;
}
else
{
if (channelCount == 1)
{
if (channelSize > 8)
{
pixelFormat = PixelFormat.Format16bppGrayScale;
dataType = DataType.GDT_Int16;
pixelSpace = 2;
}
else
{
pixelFormat = PixelFormat.Format24bppRgb;
channelCount = 3;
dataType = DataType.GDT_Byte;
pixelSpace = 3;
}
}
else
{
if (hasAlpha)
{
if (channelSize > 8)
{
pixelFormat = PixelFormat.Format64bppArgb;
dataType = DataType.GDT_UInt16;
pixelSpace = 8;
}
else
//.........這裏部分代碼省略.........
示例9: SaveBitmapGrayDirect
private static void SaveBitmapGrayDirect(Dataset ds, string filename, int iOverview)
{
// Get the GDAL Band objects from the Dataset
Band band = ds.GetRasterBand(1);
if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
band = band.GetOverview(iOverview);
// Get the width and height of the Dataset
int width = band.XSize;
int height = band.YSize;
// Create a Bitmap to store the GDAL image in
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
DateTime start = DateTime.Now;
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
// Use GDAL raster reading methods to read the image data directly into the Bitmap
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
try
{
ColorPalette pal = bitmap.Palette;
for(int i = 0; i < 256; i++)
pal.Entries[i] = Color.FromArgb( 255, i, i, i );
bitmap.Palette = pal;
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
band.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 1, stride);
TimeSpan renderTime = DateTime.Now - start;
Console.WriteLine("SaveBitmapDirect fetch time: " + renderTime.TotalMilliseconds + " ms");
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}
示例10: SaveBitmapPaletteDirect
private static void SaveBitmapPaletteDirect(Dataset ds, string filename, int iOverview)
{
// Get the GDAL Band objects from the Dataset
Band band = ds.GetRasterBand(1);
if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
band = band.GetOverview(iOverview);
ColorTable ct = band.GetRasterColorTable();
if (ct == null)
{
Console.WriteLine(" Band has no color table!");
return;
}
if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
{
Console.WriteLine(" Only RGB palette interp is supported by this sample!");
return;
}
// Get the width and height of the Dataset
int width = band.XSize;
int height = band.YSize;
// Create a Bitmap to store the GDAL image in
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
DateTime start = DateTime.Now;
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
// Use GDAL raster reading methods to read the image data directly into the Bitmap
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
try
{
int iCol = ct.GetCount();
ColorPalette pal = bitmap.Palette;
for (int i = 0; i < iCol; i++)
{
ColorEntry ce = ct.GetColorEntry(i);
pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3);
}
bitmap.Palette = pal;
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
band.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 1, stride);
TimeSpan renderTime = DateTime.Now - start;
Console.WriteLine("SaveBitmapDirect fetch time: " + renderTime.TotalMilliseconds + " ms");
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}
示例11: SaveBitmapDirect
private static void SaveBitmapDirect(Dataset ds, string filename, int iOverview)
{
// Get the GDAL Band objects from the Dataset
Band redBand = ds.GetRasterBand(1);
if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_PaletteIndex)
{
SaveBitmapPaletteDirect(ds, filename, iOverview);
return;
}
if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex)
{
SaveBitmapGrayDirect(ds, filename, iOverview);
return;
}
if (ds.RasterCount < 3)
{
Console.WriteLine("The number of the raster bands is not enough to run this sample");
System.Environment.Exit(-1);
}
if (iOverview >= 0 && redBand.GetOverviewCount() > iOverview)
redBand = redBand.GetOverview(iOverview);
Band greenBand = ds.GetRasterBand(2);
if (iOverview >= 0 && greenBand.GetOverviewCount() > iOverview)
greenBand = greenBand.GetOverview(iOverview);
Band blueBand = ds.GetRasterBand(3);
if (iOverview >= 0 && blueBand.GetOverviewCount() > iOverview)
blueBand = blueBand.GetOverview(iOverview);
// Get the width and height of the Dataset
int width = redBand.XSize;
int height = redBand.YSize;
// Create a Bitmap to store the GDAL image in
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
DateTime start = DateTime.Now;
// Use GDAL raster reading methods to read the image data directly into the Bitmap
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
try
{
int stride = bitmapData.Stride;
IntPtr buf = bitmapData.Scan0;
blueBand.ReadRaster(0, 0, width, height, buf, width, height, DataType.GDT_Byte, 4, stride);
greenBand.ReadRaster(0, 0, width, height, new IntPtr(buf.ToInt32()+1), width, height, DataType.GDT_Byte, 4, stride);
redBand.ReadRaster(0, 0, width, height, new IntPtr(buf.ToInt32()+2), width, height, DataType.GDT_Byte, 4, stride);
TimeSpan renderTime = DateTime.Now - start;
Console.WriteLine("SaveBitmapDirect fetch time: " + renderTime.TotalMilliseconds + " ms");
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(filename);
}
示例12: getBitmapRGBBandBuffered
private static Bitmap getBitmapRGBBandBuffered(Dataset ds)
{
Band redBand = ds.GetRasterBand(1);
Band greenBand = ds.GetRasterBand(2);
Band blueBand = ds.GetRasterBand(3);
int width = redBand.XSize;
int height = redBand.YSize;
DateTime start = DateTime.Now;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
byte[] r = new byte[width * height];
byte[] g = new byte[width * height];
byte[] b = new byte[width * height];
redBand.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
greenBand.ReadRaster(0, 0, width, height, g, width, height, 0, 0);
blueBand.ReadRaster(0, 0, width, height, b, width, height, 0, 0);
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
// r g b
int rColor = Convert.ToInt32(r[i + j * width]);
int gColor = Convert.ToInt32(g[i + j * width]);
int bColor = Convert.ToInt32(b[i + j * width]);
Color newColor = Color.FromArgb(rColor, gColor, bColor);
bitmap.SetPixel(i, j, newColor);
}
}
TimeSpan renderTime = DateTime.Now - start;
double milliSeconds = renderTime.TotalMilliseconds; // ms
//MessageBox.Show("GCI_RGB");
return bitmap;
}
示例13: getBitmapPaletteBuffered
private static Bitmap getBitmapPaletteBuffered(Dataset ds)
{
Band band = ds.GetRasterBand(1);
ColorTable ct = band.GetRasterColorTable();
if (ct == null)
{
return null;
}
if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
{
//MessageBox.Show(" Only RGB palette interp is supported by this sample!");
return null;
}
int width = band.XSize;
int height = band.YSize;
DateTime start = DateTime.Now;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
// entry c1 c2 c3
ColorEntry entry = ct.GetColorEntry(r[i + j * width]);
int rColor = Convert.ToInt32(entry.c1);
int gColor = Convert.ToInt32(entry.c2);
int bColor = Convert.ToInt32(entry.c3);
Color newColor = Color.FromArgb(rColor, gColor, bColor);
bitmap.SetPixel(i, j, newColor);
}
}
TimeSpan renderTime = DateTime.Now - start;
double milliSeconds = renderTime.TotalMilliseconds; // ms
//MessageBox.Show("GCI_PaletteIndex");
return bitmap;
}
示例14: SaveBitmapGrayBuffered
private static void SaveBitmapGrayBuffered(Dataset ds, string filename, int iOverview)
{
// Get the GDAL Band objects from the Dataset
Band band = ds.GetRasterBand(1);
if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
band = band.GetOverview(iOverview);
// Get the width and height of the Dataset
int width = band.XSize;
int height = band.YSize;
// Create a Bitmap to store the GDAL image in
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
DateTime start = DateTime.Now;
byte[] r = new byte[width * height];
band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
TimeSpan renderTime = DateTime.Now - start;
Console.WriteLine("SaveBitmapBuffered fetch time: " + renderTime.TotalMilliseconds + " ms");
int i, j;
for (i = 0; i< width; i++)
{
for (j=0; j<height; j++)
{
Color newColor = Color.FromArgb(Convert.ToInt32(r[i+j*width]),Convert.ToInt32(r[i+j*width]), Convert.ToInt32(r[i+j*width]));
bitmap.SetPixel(i, j, newColor);
}
}
bitmap.Save(filename);
}
示例15: GetPreview
//.........這裏部分代碼省略.........
try
{
unsafe
{
// turn everything yellow, so we can make fill transparent
for (int y = 0; y < bitmapHeight; y++)
{
byte* brow = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
for (int x = 0; x < bitmapLength; x++)
{
brow[x * 3 + 0] = 0;
brow[x * 3 + 1] = 255;
brow[x * 3 + 2] = 255;
}
}
// create 3 dimensional buffer [band][x pixel][y pixel]
double[][] tempBuffer = new double[Bands][];
double[][][] buffer = new double[Bands][][];
for (int i = 0; i < Bands; i++)
{
buffer[i] = new double[displayImageLength][];
for (int j = 0; j < displayImageLength; j++)
buffer[i][j] = new double[displayImageHeight];
}
Band[] band = new Band[Bands];
int[] ch = new int[Bands];
// get data from image
for (int i = 0; i < Bands; i++)
{
tempBuffer[i] = new double[displayImageLength * displayImageHeight];
band[i] = dataset.GetRasterBand(i + 1);
band[i].ReadRaster(
(int)imageTL.X,
(int)imageTL.Y,
(int)(imageBR.X - imageTL.X),
(int)(imageBR.Y - imageTL.Y),
tempBuffer[i], displayImageLength, displayImageHeight, 0, 0);
// parse temp buffer into the image x y value buffer
long pos = 0;
for (int y = 0; y < displayImageHeight; y++)
{
for (int x = 0; x < displayImageLength; x++, pos++)
buffer[i][x][y] = tempBuffer[i][pos];
}
if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_BlueBand) ch[i] = 0;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_GreenBand) ch[i] = 1;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_RedBand) ch[i] = 2;
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_Undefined) ch[i] = 3; // infrared
else if (band[i].GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex) ch[i] = 0;
else ch[i] = -1;
}
// store these values to keep from having to make slow method calls
int bitmapTLX = bitmapTL.X;
int bitmapTLY = bitmapTL.Y;
double imageTop = imageTL.Y;
double imageLeft = imageTL.X;
double dblMapPixelWidth = map.PixelWidth;
double dblMapPixelHeight = map.PixelHeight;
double dblMapMinX = map.Envelope.Min.X;