C#+ArcEngine:生成图像( ArGIS10.1、VS2010 窗体+代码)

分享给有需要的人,代码质量勿喷。

1、生成JPG图像

internal void xjOutputJPG(Hashtable HtNor, LInfo xjlLASInfo, string ExportLasFullName)
{
    int SumRow = xjlLASInfo.SumRow;
    int SumCol = xjlLASInfo.SumCol;
    Bitmap xjBitmap = new Bitmap(SumCol, SumRow);
    for (int i = 0; i < SumCol; i++)//列数
    {
        for (int j = 0; j < SumRow; j++)//行数
        {
            int GridIndex = (SumRow - j - 1) * SumCol + (i + 1);
            Byte GridValueTempByte = Convert.ToByte(HtNor[GridIndex]);
            xjBitmap.SetPixel(i, j, Color.FromArgb(GridValueTempByte, GridValueTempByte, GridValueTempByte));
        }
    }
    xjBitmap.Save(ExportLasFullName);
}

2、生成TIFF图像

internal void xjOutputTiff(Hashtable HtNor, LInfo xjLASInfo, string ExportLasFullName)
{
    string xjOutRasterPath = System.IO.Path.GetDirectoryName(ExportLasFullName);
    IRasterDataset xjOutRasterDataset = CreateRasterDataset(xjOutRasterPath, ExportLasFullName, xjLASInfo, HtNor);
    IRasterLayer xjOutRasterLayer = new RasterLayerClass();
    xjOutRasterLayer.CreateFromDataset(xjOutRasterDataset);
    ILayer xjOutLayer = xjOutRasterLayer;
    xjOutRasterLayer.Name = ExportLasFullName;
}
private IRasterDataset CreateRasterDataset(string FileDirectoryName, string FielName, 
                                           LInfo xjLASInfo, Hashtable G_Hashtable)
{
    //原点
    IPoint xjOriginPoint = new PointClass();
    xjOriginPoint.PutCoords(xjLASInfo.G_Xmin, xjLASInfo.G_Ymin);

    //创建数据集
    IWorkspaceFactory xjWorkspaceFactory = new RasterWorkspaceFactoryClass();
    IRasterWorkspace2 xjRasterWorkspace = xjWorkspaceFactory.OpenFromFile(FileDirectoryName, 0) as IRasterWorkspace2;
    IRasterDataset xjRasterDataset = xjRasterWorkspace.CreateRasterDataset(FielName, "TIFF", 
          xjOriginPoint, xjLASInfo.G_ColumnSum, xjLASInfo.G_RowSum, xjLASInfo.G_GridSide, xjLASInfo.G_GridSide, 
          1, rstPixelType.PT_DOUBLE, new UnknownCoordinateSystemClass(), true);


    IRasterBandCollection xjRasterBandCollection = (IRasterBandCollection)xjRasterDataset;
    IRasterBand xjRasterBand = xjRasterBandCollection.Item(0);
    IRasterProps xjRasterProps = (IRasterProps)xjRasterBand;
    xjRasterProps.NoDataValue = -1.0;

    IPnt RD_PntPixelBlockOrigin = new DblPntClass();
    RD_PntPixelBlockOrigin.SetCoords(0, 0);
    IPnt RD_PntPixelBlockSize = new DblPntClass();
    RD_PntPixelBlockSize.SetCoords(xjRasterProps.Width, xjRasterProps.Height);//(列数,行数)

    IRawPixels xjRawPixels = (IRawPixels)xjRasterBandCollection.Item(0);
    IPixelBlock3 xjPixelBlock3 = (IPixelBlock3)xjRawPixels.CreatePixelBlock(RD_PntPixelBlockSize);
    xjRawPixels.Read(RD_PntPixelBlockOrigin, (IPixelBlock)xjPixelBlock3);

    System.Array xjPixelData = (System.Array)xjPixelBlock3.get_PixelDataByRef(0);
    for (int i = 0; i < xjRasterProps.Width; i++)//列数
    {
        for (int j = 0; j < xjRasterProps.Height; j++)//行数
        {
            int GridIndex = (xjLASInfo.G_RowSum - j - 1) * xjLASInfo.G_ColumnSum + (i + 1);

            if (G_Hashtable.Contains(GridIndex))
            {
                double GridValueTemp = Convert.ToDouble(G_Hashtable[GridIndex]);
                xjPixelData.SetValue(GridValueTemp, i, j);
            }
            else
            {
                xjPixelData.SetValue(-1.0, i, j);
            }
        }
    }

    xjPixelBlock3.set_PixelData(0, (System.Object)xjPixelData);
    byte[] xjNoDataArray = (byte[])xjPixelBlock3.get_NoDataMaskByRef(0);
    xjPixelBlock3.set_NoDataMask(0, (System.Object)xjNoDataArray);

    System.Object xjCachePointer = xjRawPixels.AcquireCache();
    xjRawPixels.Write(RD_PntPixelBlockOrigin, (IPixelBlock)xjPixelBlock3);
    xjRawPixels.ReturnCache(xjCachePointer);

    return xjRasterDataset;
}
发布了63 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/xinjiang666/article/details/88674156