C# DEM数据转换为JPEG

string pathToWorkspace = System.IO.Path.GetDirectoryName(fullPath);//fullPath表示DEM所在文件夹
string demName = System.IO.Path.GetFileName(fullPath);
IWorkspaceFactory pWSFact = new RasterWorkspaceFactoryClass();
IWorkspace pWS = pWSFact.OpenFromFile(pathToWorkspace, 0);
IRasterWorkspace pRasterWorkspace = pWS as IRasterWorkspace;
IRasterLayer pRasterLayer = new RasterLayerClass();

try
{

IRaster clipRaster = pRasterWorkspace.OpenRasterDataset(demName).CreateDefaultRaster();
IRasterBandCollection pbandcollection = clipRaster as IRasterBandCollection;
IRasterBand pband = pbandcollection.Item(0);
IRasterStatistics pStatistics=pband.Statistics;
int cellmax = (int)pStatistics.Maximum;
int cellmin = (int)pStatistics.Minimum;

IRasterProps rasterProps = clipRaster as IRasterProps;

int dHeight = rasterProps.Height;//当前栅格数据集的行数

int dWidth = rasterProps.Width; //当前栅格数据集的列数

double dX = rasterProps.MeanCellSize().X; //栅格的宽度

double dY = rasterProps.MeanCellSize().Y; //栅格的高度

IEnvelope extent = rasterProps.Extent; //当前栅格数据集的范围

rstPixelType pixelType = rasterProps.PixelType; //当前栅格像素类型

IPnt pntSize = new PntClass();

pntSize.SetCoords(dX, dY);

IPixelBlock pixelBlock = clipRaster.CreatePixelBlock(pntSize);

IPnt pnt = new PntClass();

//Bitmap myjpg = new Bitmap(dWidth, dHeight,PixelFormat.Format8bppIndexed);
Bitmap myjpg = new Bitmap(dWidth, dHeight);
Console.WriteLine(DateTime.Now.ToString());
for (int y = 0; y < dHeight; y++)

for (int x = 0; x < dWidth; x++)
{

pnt.SetCoords(x,y );

clipRaster.Read(pnt, pixelBlock);//就是这里的读取耗时了

if (pixelBlock != null)
{

object obj = pixelBlock.GetVal(0, 0, 0);
int rgb0 = Convert.ToInt16(obj);//这里获取的是高程值
obj = null;
if (rgb0 != 0)
{
////高程值越大,颜色越趋向于白色
//int rgb = 255 * (rgb0 - cellmin) / (cellmax - cellmin);
//Color pixelColor = Color.FromArgb(rgb, rgb, rgb);
//myjpg.SetPixel(x, y, pixelColor);


//高程值越大,颜色越趋向于黑色
int rgb = 255 * (cellmax - rgb0) / (cellmax - cellmin);
Color pixelColor = Color.FromArgb(rgb, rgb, rgb);
myjpg.SetPixel(x, y, pixelColor);
}
else
{
////高程值越大,颜色越趋向于白色,高=0时 为黑色
//Color pixelColor = Color.FromArgb(0, 0, 0);


//高程值越大,颜色越趋向于黑色,高=0时 为白色
Color pixelColor = Color.FromArgb(255, 255, 255);
myjpg.SetPixel(x, y, pixelColor);
}
}

}
myjpg.Save(@"E:\yzdtest\data\myjpg.jpg");
Console.WriteLine(DateTime.Now.ToString());
MessageBox.Show("读取DEM完毕成功");
}
catch (Exception err)
{
MessageBox.Show(err.Message);

}

作者: 风云

出处: http://www.cnblogs.com/fengyunlishi/

本文版权归风云和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

posted @ 2013-05-09 22:29  风云@历史  阅读(627)  评论(0编辑  收藏  举报