private void calcPolygon() { try { if (_tfw == null) { FileInfo fi = new FileInfo(_filename); string tfwfilename = _filename.Substring(0, _filename.Length - fi.Extension.Length); if (fi.Extension.ToLower() == ".jpg") { tfwfilename += ".jgw"; } else { tfwfilename += ".tfw"; } _tfw = new TFWFile(tfwfilename); } if (_iWidth == 0 || _iHeight == 0) { using (var image = GraphicsEngine.Current.Engine.CreateBitmap(_filename)) { SetBounds(image); } } } catch { _polygon = null; } }
private bool calcPolygon() { if (!InitReader()) { return(false); } TFWFile tfw = this.GeoCoord as TFWFile; if (tfw == null) { return(false); } int iWidth = _geoCoord.iWidth; int iHeight = _geoCoord.iHeight; _polygon = new Polygon(); Ring ring = new Ring(); gView.Framework.Geometry.Point p1 = new gView.Framework.Geometry.Point( tfw.X - tfw.dx_X / 2.0 - tfw.dy_X / 2.0, tfw.Y - tfw.dx_Y / 2.0 - tfw.dy_Y / 2.0); ring.AddPoint(p1); ring.AddPoint(new gView.Framework.Geometry.Point(p1.X + tfw.dx_X * iWidth, p1.Y + tfw.dx_Y * iWidth)); ring.AddPoint(new gView.Framework.Geometry.Point(p1.X + tfw.dx_X * iWidth + tfw.dy_X * iHeight, p1.Y + tfw.dx_Y * iWidth + tfw.dy_Y * iHeight)); ring.AddPoint(new gView.Framework.Geometry.Point(p1.X + tfw.dy_X * iHeight, p1.Y + tfw.dy_Y * iHeight)); _polygon.AddRing(ring); return(true); }
public RasterFileClass(IRasterDataset dataset, string filename, IPolygon polygon) { try { FileInfo fi = new FileInfo(filename); _title = fi.Name; _filename = filename; _dataset = dataset; string tfwfilename = filename.Substring(0, filename.Length - fi.Extension.Length); if (fi.Extension.ToLower() == ".jpg") { tfwfilename += ".jgw"; } else { tfwfilename += ".tfw"; } _tfw = new TFWFile(tfwfilename); //if (!_tfw.isValid) //{ // _valid = false; // return; //} FileInfo fiPrj = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".prj"); if (fiPrj.Exists) { StreamReader sr = new StreamReader(fiPrj.FullName); string wkt = sr.ReadToEnd(); sr.Close(); _sRef = gView.Framework.Geometry.SpatialReference.FromWKT(wkt); } if (polygon != null) { _polygon = polygon; } else { calcPolygon(); } } catch { _valid = false; } }
private void calcPolygon() { try { if (_tfw == null) { FileInfo fi = new FileInfo(_filename); string tfwfilename = _filename.Substring(0, _filename.Length - fi.Extension.Length); if (fi.Extension.ToLower() == ".jpg") { tfwfilename += ".jgw"; } else { tfwfilename += ".tfw"; } _tfw = new TFWFile(tfwfilename); } Bitmap image = null; if (_iWidth == 0 || _iHeight == 0) { image = (Bitmap)Bitmap.FromFile(_filename); } setBounds(image); if (image != null) { image.Dispose(); image = null; } } catch { _polygon = null; } }
public bool InsertImageDatasetBitmap(int image_id, IRasterLayer layer, int levels, System.Drawing.Imaging.ImageFormat format) { if (_conn == null) { _errMsg = "Not Connected (use Open...)"; return(false); } if (layer.RasterClass == null) { _errMsg = "No Rasterclass..."; return(false); } DataTable tab = _conn.Select("*", "FDB_Datasets", "Name='" + _dsname + "' AND ImageDataset=1"); if (tab == null) { _errMsg = _conn.errorMessage; return(false); } if (tab.Rows.Count == 0) { _errMsg = "Can't find ImageDataset"; return(false); } string imageSpace = tab.Rows[0]["ImageSpace"].ToString(); if (imageSpace.Equals("unmanaged")) { return(true); } if (imageSpace.Equals(System.DBNull.Value) || imageSpace.Equals("database") || imageSpace.Equals("")) { if (!(layer is IBitmap)) { _errMsg = "Layer don't implement the IBitmap Interface..."; return(false); } System.Drawing.Bitmap bm = ((IBitmap)layer).LoadBitmap(); if (bm == null) { _errMsg = "Can't load Bitmap..."; return(false); } TFWFile tfw = new TFWFile( layer.RasterClass.oX, layer.RasterClass.oY, layer.RasterClass.dx1, layer.RasterClass.dx2, layer.RasterClass.dy1, layer.RasterClass.dy2); Pyramid pyramid = new Pyramid(); if (pyramid.Create(image_id, _dsname, bm, tfw, _conn, levels, format)) { return(true); } else { _errMsg = pyramid.lastErrorMessage; return(false); } } else { try { if (!(layer is IRasterFile)) { _errMsg = "Can't get Filename from Rasterlayer..."; return(false); } FileInfo fi = new FileInfo(((IRasterFile)layer).Filename); if (!fi.Exists) { _errMsg = "Rasterfile does not exist..."; return(false); } DirectoryInfo di = new DirectoryInfo(imageSpace); if (!di.Exists) { di.Create(); } Pyramid pyramid = new Pyramid(); string filename = di.FullName + @"\pyr_" + System.Guid.NewGuid().ToString("N"); if (pyramid.Create(fi.FullName, filename, levels, format)) { DataTable fc_tab = _conn.Select("FDB_OID,MANAGED_FILE", "FC_" + _dsname + "_IMAGE_POLYGONS", "FDB_OID=" + image_id, "", true); if (fc_tab == null || tab.Rows.Count != 1) { _errMsg = _conn.errorMessage; return(false); } fc_tab.Rows[0]["MANAGED_FILE"] = pyramid.FileName.ToLower().Replace(imageSpace.ToLower(), ""); if (!_conn.Update(fc_tab)) { _errMsg = _conn.errorMessage; return(false); } return(true); } else { _errMsg = pyramid.lastErrorMessage; return(false); } } catch (Exception ex) { _errMsg = "Check Imagespace: " + ex.Message; return(false); } } }
public bool Create(string sourceFilename, string destinationFilename, int levels, ImageFormat format) { Bitmap8pbbIndexed sourceBitmap = null; Bitmap image = null; try { FileInfo fi = new FileInfo(sourceFilename); string tfwfilename = sourceFilename.Substring(0, sourceFilename.Length - fi.Extension.Length); if (fi.Extension.ToLower() == ".jpg" || fi.Extension.ToLower() == ".jpeg") { tfwfilename += ".jgw"; } else { tfwfilename += ".tfw"; } TFWFile tfw = new TFWFile(tfwfilename); image = new Bitmap(sourceFilename); //Image.FromFile(sourceFilename); Polygon polygon = tfw.CreatePolygon(image.Width, image.Height); StreamWriter writer = new StreamWriter(destinationFilename + ".pyx"); double X, Y, dx1, dx2, dy1, dy2, cellX, cellY; int width, height; // Header schreiben PyramidFileHeader header = new PyramidFileHeader(true); header.iWidth = width = image.Width; header.iHeight = height = image.Height; header.X = X = tfw.X; header.Y = Y = tfw.Y; header.dx1 = dx1 = tfw.dx_X; header.dx2 = dx2 = tfw.dx_Y; header.dy1 = dy1 = tfw.dy_X; header.dy2 = dy2 = tfw.dy_Y; header.cellX = cellX = tfw.cellX; header.cellY = cellY = tfw.cellY; header.Levels = levels; header.Format = format.Guid; header.Save(writer.BaseStream); X = X - dx1 / 2.0 - dy1 / 2.0; // Auf die linke oberere Ecke setzen Y = Y - dx2 / 2.0 - dy2 / 2.0; PixelFormat pixFormat = image.PixelFormat; if (pixFormat != PixelFormat.Format8bppIndexed && pixFormat != PixelFormat.Format1bppIndexed) { pixFormat = PixelFormat.Format32bppArgb; } else { sourceBitmap = new Bitmap8pbbIndexed(image); } // Levelheader schreiben for (int lev = 1; lev <= levels; lev++) { int tWidth = width / (int)Math.Pow(2, lev - 1), tHeight = height / (int)Math.Pow(2, lev - 1); int iWidth = getWidth(tWidth); int iHeight = getHeight(tHeight); double stepX = iWidth * Math.Pow(2, lev - 1), stepY = iHeight * Math.Pow(2, lev - 1); PyramidLevelHeader levelHeader; levelHeader.level = lev; levelHeader.numPictures = 0; // ???? levelHeader.cellX = cellX * Math.Pow(2, lev - 1);; levelHeader.cellY = cellY * Math.Pow(2, lev - 1);; for (int y = 0, yC = 0; y < tHeight; y += iHeight, yC++) { for (int x = 0, xC = 0; x < tWidth; x += iWidth, xC++) { levelHeader.numPictures++; } } levelHeader.Save(writer.BaseStream); } // Pictureheader und Bilder schreiben StreamWriter picWriter = new StreamWriter(destinationFilename + ".pyc"); for (int lev = 1; lev <= levels; lev++) { int tWidth = width / (int)Math.Pow(2, lev - 1), tHeight = height / (int)Math.Pow(2, lev - 1); int iWidth = getWidth(tWidth); int iHeight = getHeight(tHeight); double stepX = iWidth * Math.Pow(2, lev - 1), stepY = iHeight * Math.Pow(2, lev - 1); for (int y = 0, yC = 0; y < tHeight; y += iHeight, yC++) { for (int x = 0, xC = 0; x < tWidth; x += iWidth, xC++) { PyramidPictureHeader picHeader; using (Bitmap bm = new Bitmap(Math.Min(iWidth, tWidth - x), Math.Min(iHeight, tHeight - y), image.PixelFormat)) { if ((bm.PixelFormat == PixelFormat.Format1bppIndexed || bm.PixelFormat == PixelFormat.Format8bppIndexed) && sourceBitmap != null) { Bitmap8pbbIndexed destBitmap = new Bitmap8pbbIndexed(bm); destBitmap.DrawImage(sourceBitmap, new Rectangle(0, 0, bm.Width, bm.Height), new Rectangle( (int)(xC * (float)stepX), (int)(yC * (float)stepY), (int)Math.Min(image.Width - xC * stepX, stepX), (int)Math.Min(image.Height - yC * stepY, stepY) )); destBitmap.Dispose(false); bm.Palette = image.Palette; } else { using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bm)) { gr.DrawImage(image, new Rectangle(0, 0, bm.Width, bm.Height), xC * (float)stepX, yC * (float)stepY, (float)Math.Min(image.Width - xC * stepX, stepX), (float)Math.Min(image.Height - yC * stepY, stepY), GraphicsUnit.Pixel); } } MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bm.Save(bw.BaseStream, format); byte[] buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); bw.Close(); ms.Close(); bm.Dispose(); picHeader.startPosition = picWriter.BaseStream.Position; picHeader.streamLength = buffer.Length; picWriter.BaseStream.Write(buffer, 0, buffer.Length); } picHeader.level = lev; picHeader.iWidth = iWidth; picHeader.iHeight = iHeight; picHeader.dx1 = dx1 * Math.Pow(2, lev - 1); picHeader.dx2 = dx2 * Math.Pow(2, lev - 1); picHeader.dy1 = dy1 * Math.Pow(2, lev - 1); picHeader.dy2 = dy2 * Math.Pow(2, lev - 1); picHeader.X = X + picHeader.dx1 * x + picHeader.dy1 * y + picHeader.dx1 / 2.0 + picHeader.dy1 / 2.0; picHeader.Y = Y + picHeader.dx2 * x + picHeader.dy2 * y + picHeader.dx2 / 2.0 + picHeader.dy2 / 2.0; picHeader.cellX = cellX * Math.Pow(2, lev - 1); picHeader.cellY = cellY * Math.Pow(2, lev - 1); picHeader.Save(writer.BaseStream); } } } picWriter.Close(); writer.Close(); return(true); } catch (Exception ex) { _errMsg = ex.Message; if (sourceBitmap != null) { sourceBitmap.Dispose(false); sourceBitmap = null; } if (image != null) { image.Dispose(); image = null; } return(false); } }
/* * public bool Create_mdb_(string sourceFilename, string destinationFilename, int levels,ImageFormat format) * { * Bitmap8pbbIndexed sourceBitmap = null; * Bitmap image = null; * try * { * FileInfo fi = new FileInfo(sourceFilename); * string tfwfilename = sourceFilename.Substring(0, sourceFilename.Length - fi.Extension.Length); * if (fi.Extension.ToLower() == ".jpg" || fi.Extension.ToLower()==".jpeg") * tfwfilename += ".jgw"; * else * tfwfilename += ".tfw"; * TFWFile tfw = new TFWFile(tfwfilename); * * string ext = ""; * if(format==ImageFormat.Jpeg) * { * ext = ".jpg.mdb"; * } * else if(format==ImageFormat.Png) * { * ext = ".png.mdb"; * } * else if (format == ImageFormat.Tiff) * { * ext = ".tif.mdb"; * } * else * { * format = ImageFormat.Jpeg; * ext = ".jpg.mdb"; * } * fi = new FileInfo(destinationFilename); * _filename = destinationFilename = destinationFilename.Substring(0, destinationFilename.Length - fi.Extension.Length) + ext; * fi = new FileInfo(destinationFilename); * if (fi.Exists) fi.Delete(); * * ADOX.CatalogClass cat = new ADOX.CatalogClass(); * cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + destinationFilename + ";Jet OLEDB:Engine Type=5"); * //System.Runtime.InteropServices.Marshal.ReleaseComObject(cat); * cat = null; * * DBConnection conn = new DBConnection(); * conn.OleDbConnectionMDB = destinationFilename; * * string[] fields_main ={ "SHAPE", "UNIT", "X", "Y", "dx1", "dx2", "dy1", "dy2", "cellX", "cellY", "iWidth", "iHeight" }; * string[] types_main ={ "OLEOBJECT", "TEXT(50)", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "INTEGER", "INTEGER" }; * * string[] fields_img = { "ID", "LEV", "SHAPE", "IMG", "X", "Y", "dx1", "dx2", "dy1", "dy2", "cellX", "cellY", "iWidth", "iHeight" }; * string[] types_img = { "INTEGER NOT NULL IDENTITY(1,1)", "INTEGER", "OLEOBJECT", "OLEOBJECT", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "INTEGER", "INTEGER" }; * * if (!conn.createTable("PYRAMID_SHAPE", fields_main, types_main)) return false; * if (!conn.createTable("PYRAMID_IMG", fields_img, types_img)) return false; * * image = new Bitmap(sourceFilename); //Image.FromFile(sourceFilename); * Polygon polygon = tfw.CreatePolygon(image.Width, image.Height); * * BinaryWriter writer = new BinaryWriter(new MemoryStream()); * polygon.Serialize(writer,new GeometryDef(geometryType.Polygon,null,true)); * * byte[] geometry = new byte[writer.BaseStream.Length]; * writer.BaseStream.Position = 0; * writer.BaseStream.Read(geometry, (int)0, (int)writer.BaseStream.Length); * writer.Close(); * * DataTable tab = conn.Select("*", "PYRAMID_SHAPE", "", "", true); * DataRow row = tab.NewRow(); * row["SHAPE"] = geometry; * row["UNIT"] = "Meters"; * double X, Y, dx1, dx2, dy1, dy2, cellX, cellY; * int width, height; * * row["X"] = X = tfw.X; * row["Y"] = Y = tfw.Y; * row["dx1"] = dx1 = tfw.dx_X; * row["dx2"] = dx2 = tfw.dx_Y; * row["dy1"] = dy1 = tfw.dy_X; * row["dy2"] = dy2 = tfw.dy_Y; * row["cellX"] = cellX = Math.Sqrt(tfw.dx_X * tfw.dx_X + tfw.dx_Y * tfw.dx_Y); * row["cellY"] = cellY = Math.Sqrt(tfw.dy_X * tfw.dy_X + tfw.dy_Y * tfw.dy_Y); * row["iWidth"] = width = image.Width; * row["iHeight"] = height = image.Height; * * tab.Rows.Add(row); * conn.Update(tab); * * X = X - dx1 / 2.0 - dy1 / 2.0; // Auf die linke oberere Ecke setzen * Y = Y - dx2 / 2.0 - dy2 / 2.0; * * PixelFormat pixFormat = image.PixelFormat; * * if (pixFormat != PixelFormat.Format8bppIndexed && * pixFormat != PixelFormat.Format1bppIndexed) * { * pixFormat = PixelFormat.Format32bppArgb; * } * else * { * sourceBitmap = new Bitmap8pbbIndexed(image); * } * * for (int lev = 1; lev <= levels; lev++) * { * int tWidth = width / (int)Math.Pow(2,lev-1), tHeight = height / (int)Math.Pow(2,lev-1); * int iWidth = getWidth(tWidth); * int iHeight = getHeight(tHeight); * double stepX = iWidth*Math.Pow(2,lev-1), stepY = iHeight*Math.Pow(2,lev-1); * * for (int y = 0,yC=0; y < tHeight; y += iHeight,yC++) * { * for (int x = 0,xC=0; x < tWidth; x += iWidth,xC++) * { * //int ww = Math.Min(iWidth, tWidth - x); * //int hh = Math.Min(iHeight, tHeight - y); * //if (ww < 10 || hh < 10) * //{ * // continue; * //} * using (Bitmap bm = new Bitmap(Math.Min(iWidth, tWidth - x), Math.Min(iHeight, tHeight - y),image.PixelFormat)) * { * if ((bm.PixelFormat == PixelFormat.Format1bppIndexed || * bm.PixelFormat == PixelFormat.Format8bppIndexed) && * sourceBitmap != null) * { * Bitmap8pbbIndexed destBitmap = new Bitmap8pbbIndexed(bm); * destBitmap.DrawImage(sourceBitmap, * new Rectangle(0, 0, bm.Width, bm.Height), * new Rectangle( * (int)(xC * (float)stepX), (int)(yC * (float)stepY), * (int)Math.Min(image.Width - xC * stepX, stepX), * (int)Math.Min(image.Height - yC * stepY, stepY) * )); * destBitmap.Dispose(false); * bm.Palette = image.Palette; * } * else * { * using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bm)) * { * gr.DrawImage(image, * new Rectangle(0, 0, bm.Width, bm.Height), * xC * (float)stepX, yC * (float)stepY, * (float)Math.Min(image.Width - xC * stepX, stepX), * (float)Math.Min(image.Height - yC * stepY, stepY), * GraphicsUnit.Pixel); * } * } * * tab = conn.Select("*", "PYRAMID_IMG", "ID=-1", "", true); * row = tab.NewRow(); * * writer = new BinaryWriter(new MemoryStream()); * bm.Save(writer.BaseStream, format); * //bm.Save(sourceFilename + "_" + lev + "_" + xC + "_" + yC + ".jpg", ImageFormat.Jpeg); * byte[] img = new byte[writer.BaseStream.Length]; * writer.BaseStream.Position = 0; * writer.BaseStream.Read(img, (int)0, (int)writer.BaseStream.Length); * writer.Close(); * * row["LEV"] = lev; * row["IMG"] = img; * //row["X"] = X + dx1 * stepX * xC + dy1 * stepY * yC; * //row["Y"] = Y + dy2 * stepY * yC + dx2 * stepX * xC; * * row["dx1"] = dx1*Math.Pow(2,lev-1); * row["dx2"] = dx2*Math.Pow(2,lev-1); * row["dy1"] = dy1*Math.Pow(2,lev-1); * row["dy2"] = dy2*Math.Pow(2,lev-1); * * row["X"] = X + (double)row["dx1"] * x + (double)row["dy1"] * y + (double)row["dx1"] / 2.0 + (double)row["dy1"] / 2.0; * row["Y"] = Y + (double)row["dx2"] * x + (double)row["dy2"] * y + (double)row["dx2"] / 2.0 + (double)row["dy2"] / 2.0; * * row["cellX"] = cellX * Math.Pow(2, lev - 1); * row["cellY"] = cellY * Math.Pow(2, lev - 1); * row["iWidth"] = bm.Width; * row["iHeight"] = bm.Height; * * tfw = new TFWFile( * (double)row["X"], * (double)row["Y"], * (double)row["dx1"], * (double)row["dx2"], * (double)row["dy1"], * (double)row["dy2"]); * * polygon = tfw.CreatePolygon(bm.Width, bm.Height); * writer = new BinaryWriter(new MemoryStream()); * polygon.Serialize(writer, new GeometryDef(geometryType.Polygon,null,true)); * * geometry = new byte[writer.BaseStream.Length]; * writer.BaseStream.Position = 0; * writer.BaseStream.Read(geometry, (int)0, (int)writer.BaseStream.Length); * writer.Close(); * row["SHAPE"] = geometry; * * tab.Rows.Add(row); * conn.Update(tab); * tab.Dispose(); * bm.Dispose(); * } * } * } * } * * conn.Dispose(); * conn = null; * image.Dispose(); * image = null; * * return true; * } * catch(Exception ex) * { * * _errMsg = ex.Message; * if (sourceBitmap != null) * { * sourceBitmap.Dispose(false); * sourceBitmap = null; * } * if (image != null) * { * image.Dispose(); * image = null; * } * return false; * } * } */ public bool Create(int image_id, string imageDSName, Bitmap image, TFWFile TFW, ICommonDbConnection conn, int levels, ImageFormat format) { if (image == null) { return(false); } double X, Y, dx1, dx2, dy1, dy2, cellX, cellY; int width = image.Width, height = image.Height; X = TFW.X; Y = TFW.Y; dx1 = TFW.dx_X; dx2 = TFW.dx_Y; dy1 = TFW.dy_X; dy2 = TFW.dy_Y; cellX = Math.Sqrt(TFW.dx_X * TFW.dx_X + TFW.dx_Y * TFW.dx_Y); cellY = Math.Sqrt(TFW.dy_X * TFW.dy_X + TFW.dy_Y * TFW.dy_Y); X = X - dx1 / 2.0 - dy1 / 2.0; // Auf die linke oberere Ecke setzen Y = Y - dx2 / 2.0 - dy2 / 2.0; DataTable tab; if (conn.dbType == DBType.sql) { tab = conn.Select("*", imageDSName + "_IMAGE_DATA", "IMAGE_ID=-1", "", true); } else { tab = conn.Select("*", "PYRAMID_IMG", "ID=-1", "", true); } for (int lev = 1; lev <= Math.Max(1, levels); lev++) { int tWidth = width / (int)Math.Pow(2, lev - 1), tHeight = height / (int)Math.Pow(2, lev - 1); int iWidth = (levels > 0) ? getWidth(tWidth) : tWidth; int iHeight = (levels > 0) ? getHeight(tHeight) : tHeight; double stepX = iWidth * Math.Pow(2, lev - 1), stepY = iHeight * Math.Pow(2, lev - 1); for (int y = 0, yC = 0; y < tHeight; y += iHeight, yC++) { for (int x = 0, xC = 0; x < tWidth; x += iWidth, xC++) { //int ww = Math.Min(iWidth, tWidth - x); //int hh = Math.Min(iHeight, tHeight - y); //if (ww < 10 || hh < 10) //{ // continue; //} using (Bitmap bm = new Bitmap(Math.Min(iWidth, tWidth - x), Math.Min(iHeight, tHeight - y))) { using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bm)) { gr.DrawImage(image, new Rectangle(0, 0, bm.Width, bm.Height), xC * (float)stepX, yC * (float)stepY, (float)Math.Min(image.Width - xC * stepX, stepX), (float)Math.Min(image.Height - yC * stepY, stepY), GraphicsUnit.Pixel); } /* * DataTable tab; * if (conn.dbType == DBType.sql) * { * tab = conn.Select("*", imageDSName+"_IMAGE_DATA", "IMAGE_ID=-1", "", true); * } * else * { * tab = conn.Select("*", "PYRAMID_IMG", "ID=-1", "", true); * } */ DataRow row = tab.NewRow(); BinaryWriter writer = new BinaryWriter(new MemoryStream()); bm.Save(writer.BaseStream, format); //bm.Save(sourceFilename + "_" + lev + "_" + xC + "_" + yC + ".jpg", ImageFormat.Jpeg); byte[] img = new byte[writer.BaseStream.Length]; writer.BaseStream.Position = 0; writer.BaseStream.Read(img, (int)0, (int)writer.BaseStream.Length); writer.Close(); if (conn.dbType == DBType.sql) { row["IMAGE_ID"] = image_id; } row["LEV"] = lev; row[(conn.dbType == DBType.sql) ? "IMAGE" : "IMG"] = img; //row["X"] = X + dx1 * stepX * xC + dy1 * stepY * yC; //row["Y"] = Y + dy2 * stepY * yC + dx2 * stepX * xC; row["dx1"] = dx1 * Math.Pow(2, lev - 1); row["dx2"] = dx2 * Math.Pow(2, lev - 1); row["dy1"] = dy1 * Math.Pow(2, lev - 1); row["dy2"] = dy2 * Math.Pow(2, lev - 1); row["X"] = X + (double)row["dx1"] * x + (double)row["dy1"] * y + (double)row["dx1"] / 2.0 + (double)row["dy1"] / 2.0; row["Y"] = Y + (double)row["dx2"] * x + (double)row["dy2"] * y + (double)row["dx2"] / 2.0 + (double)row["dy2"] / 2.0; row["cellX"] = cellX * Math.Pow(2, lev - 1); row["cellY"] = cellY * Math.Pow(2, lev - 1); row["iWidth"] = bm.Width; row["iHeight"] = bm.Height; TFWFile tfw = new TFWFile( (double)row["X"], (double)row["Y"], (double)row["dx1"], (double)row["dx2"], (double)row["dy1"], (double)row["dy2"]); IPolygon polygon = tfw.CreatePolygon(bm.Width, bm.Height); writer = new BinaryWriter(new MemoryStream()); polygon.Serialize(writer, new GeometryDef(geometryType.Polygon, null, true)); byte[] geometry = new byte[writer.BaseStream.Length]; writer.BaseStream.Position = 0; writer.BaseStream.Read(geometry, (int)0, (int)writer.BaseStream.Length); writer.Close(); row["SHAPE"] = geometry; tab.Rows.Add(row); /* * conn.Update(tab); * tab.Dispose(); * */ bm.Dispose(); } } } } conn.Update(tab); tab.Dispose(); return(true); }
public void BeginPaint(gView.Framework.Carto.IDisplay display, ICancelTracker cancelTracker) { IntPtr bufferData = (IntPtr)0; BitmapData bitmapData = null; double mag = 1f; // mag immer als float, läuft stabiler!!! int x = 0; int y = 0; int iWidth = 0; int iHeight = 0; try { if (_reader == (IntPtr)0) { if (!InitReader()) { return; } } if (!(_polygon is ITopologicalOperation) || _reader == (IntPtr)0) { return; } TFWFile tfw = this.GeoCoord as TFWFile; if (tfw == null) { return; } IEnvelope dispEnvelope = display.DisplayTransformation.TransformedBounds(display); //display.Envelope; if (display.GeometricTransformer != null) { dispEnvelope = (IEnvelope)((IGeometry)display.GeometricTransformer.InvTransform2D(dispEnvelope)).Envelope; } IGeometry clipped; ((ITopologicalOperation)_polygon).Clip(dispEnvelope, out clipped); if (!(clipped is IPolygon)) { return; } IPolygon cPolygon = (IPolygon)clipped; // geclipptes Polygon transformieren -> Bild vector2[] vecs = new vector2[cPolygon[0].PointCount]; for (int i = 0; i < cPolygon[0].PointCount; i++) { vecs[i] = new vector2(cPolygon[0][i].X, cPolygon[0][i].Y); } if (!tfw.ProjectInv(vecs)) { return; } IEnvelope picEnv = vector2.IntegerEnvelope(vecs); picEnv.minx = Math.Max(0, picEnv.minx); picEnv.miny = Math.Max(0, picEnv.miny); picEnv.maxx = Math.Min(picEnv.maxx, _geoCoord.iWidth); picEnv.maxy = Math.Min(picEnv.maxy, _geoCoord.iHeight); // Ecken zurücktransformieren -> Welt vecs = new vector2[3]; vecs[0] = new vector2(picEnv.minx, picEnv.miny); vecs[1] = new vector2(picEnv.maxx, picEnv.miny); vecs[2] = new vector2(picEnv.minx, picEnv.maxy); tfw.Project(vecs); _p1 = new gView.Framework.Geometry.Point(vecs[0].x, vecs[0].y); _p2 = new gView.Framework.Geometry.Point(vecs[1].x, vecs[1].y); _p3 = new gView.Framework.Geometry.Point(vecs[2].x, vecs[2].y); double pix = display.mapScale / (display.dpi / 0.0254); // [m] double c1 = Math.Sqrt(_geoCoord.xRes * _geoCoord.xRes + _geoCoord.xRot * _geoCoord.xRot); double c2 = Math.Sqrt(_geoCoord.yRes * _geoCoord.yRes + _geoCoord.yRot * _geoCoord.yRot); mag = Math.Round((Math.Min(c1, c2) / pix), 8); // Immer in auf float runden! Läuft stabiler!!! //mag = (float)mag; //1.03; if (mag > 1f) { mag = 1f; } if (mag < _geoCoord.MinMagnification) { mag = (float)_geoCoord.MinMagnification; } x = (int)(picEnv.minx * mag); y = (int)(picEnv.miny * mag); iWidth = (int)((picEnv.Width - 1) * mag); iHeight = (int)((picEnv.Height - 1) * mag); bufferData = MrSidWrapper.Read(_reader, x, y, iWidth, iHeight, (double)mag); if (bufferData == (IntPtr)0) { return; } int totalWidth = MrSidWrapper.GetTotalCols(bufferData); int totalHeight = MrSidWrapper.GetTotalRows(bufferData); if (_bm != null) { _bm.Dispose(); } _bm = new Bitmap(totalWidth, totalHeight, PixelFormat.Format24bppRgb); bitmapData = _bm.LockBits(new Rectangle(0, 0, totalWidth, totalHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); Console.Write(bitmapData.Scan0); MrSidWrapper.ReadBandData(bufferData, bitmapData.Scan0, (uint)3, (uint)bitmapData.Stride); //_bm.Save(@"C:\temp\pic\" + Guid.NewGuid() + ".jpg", ImageFormat.Jpeg); } catch (Exception ex) { //string errMsg = ex.Message; EndPaint(cancelTracker); if (display is IServiceMap && ((IServiceMap)display).MapServer != null) { IMapServer mapServer = ((IServiceMap)display).MapServer; mapServer.Log( "RenderRasterLayerThread", loggingMethod.error, ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace + "\n" + "filename=" + _filename + "\n" + "x=" + x.ToString() + "\n" + "y=" + y.ToString() + "\n" + "iWidth=" + iWidth.ToString() + "\n" + "iHeight=" + iHeight.ToString() + "\n" + "mag=" + mag.ToString() + "\n"); } else { throw ex; } } finally { if (bitmapData != null) { _bm.UnlockBits(bitmapData); } MrSidWrapper.ReleaseBandData(bufferData); ReleaseReader(); } }
async public Task <IRasterPaintContext> BeginPaint(gView.Framework.Carto.IDisplay display, ICancelTracker cancelTracker) { IntPtr bufferData = (IntPtr)0; GraphicsEngine.BitmapPixelData bitmapData = null; double mag = 1f; // mag immer als float, läuft stabiler!!! int x = 0; int y = 0; int iWidth = 0; int iHeight = 0; GraphicsEngine.Abstraction.IBitmap bitmap = null; try { if (_reader == (IntPtr)0) { if (!InitReader()) { return(null); } } if (!(_polygon is ITopologicalOperation) || _reader == (IntPtr)0) { return(null); } TFWFile tfw = this.GeoCoord as TFWFile; if (tfw == null) { return(null); } IEnvelope dispEnvelope = display.DisplayTransformation.TransformedBounds(display); //display.Envelope; if (display.GeometricTransformer != null) { dispEnvelope = ((IGeometry)display.GeometricTransformer.InvTransform2D(dispEnvelope)).Envelope; } IGeometry clipped; ((ITopologicalOperation)_polygon).Clip(dispEnvelope, out clipped); if (!(clipped is IPolygon)) { return(null); } IPolygon cPolygon = (IPolygon)clipped; if (cPolygon.RingCount == 0 || cPolygon[0].Area == 0D) { return(null); } // geclipptes Polygon transformieren -> Bild vector2[] vecs = new vector2[cPolygon[0].PointCount]; for (int i = 0; i < cPolygon[0].PointCount; i++) { vecs[i] = new vector2(cPolygon[0][i].X, cPolygon[0][i].Y); } if (!tfw.ProjectInv(vecs)) { return(null); } IEnvelope picEnv = vector2.IntegerEnvelope(vecs); picEnv.minx = Math.Max(0, picEnv.minx); picEnv.miny = Math.Max(0, picEnv.miny); picEnv.maxx = Math.Min(picEnv.maxx, _geoCoord.iWidth); picEnv.maxy = Math.Min(picEnv.maxy, _geoCoord.iHeight); // Ecken zurücktransformieren -> Welt vecs = new vector2[3]; vecs[0] = new vector2(picEnv.minx, picEnv.miny); vecs[1] = new vector2(picEnv.maxx, picEnv.miny); vecs[2] = new vector2(picEnv.minx, picEnv.maxy); tfw.Project(vecs); _p1 = new gView.Framework.Geometry.Point(vecs[0].x, vecs[0].y); _p2 = new gView.Framework.Geometry.Point(vecs[1].x, vecs[1].y); _p3 = new gView.Framework.Geometry.Point(vecs[2].x, vecs[2].y); double pix = display.mapScale / (display.dpi / 0.0254); // [m] double c1 = Math.Sqrt(_geoCoord.xRes * _geoCoord.xRes + _geoCoord.xRot * _geoCoord.xRot); double c2 = Math.Sqrt(_geoCoord.yRes * _geoCoord.yRes + _geoCoord.yRot * _geoCoord.yRot); mag = Math.Round((Math.Min(c1, c2) / pix), 8); // Immer in auf float runden! Läuft stabiler!!! //mag = (float)mag; //1.03; if (mag > 1f) { mag = 1f; } if (mag < _geoCoord.MinMagnification) { mag = (float)_geoCoord.MinMagnification; } x = (int)(picEnv.minx * mag); y = (int)(picEnv.miny * mag); iWidth = (int)((picEnv.Width - 1) * mag); iHeight = (int)((picEnv.Height - 1) * mag); bufferData = MrSidWrapper.Read(_reader, x, y, iWidth, iHeight, mag); if (bufferData == (IntPtr)0) { return(null); } int totalWidth = MrSidWrapper.GetTotalCols(bufferData); int totalHeight = MrSidWrapper.GetTotalRows(bufferData); bitmap = GraphicsEngine.Current.Engine.CreateBitmap(totalWidth, totalHeight, GraphicsEngine.PixelFormat.Rgb24); bitmapData = bitmap.LockBitmapPixelData(GraphicsEngine.BitmapLockMode.WriteOnly, GraphicsEngine.PixelFormat.Rgb24); MrSidWrapper.ReadBandData(bufferData, bitmapData.Scan0, 3, (uint)bitmapData.Stride); return(new RasterPaintContext(bitmap)); } catch (Exception ex) { //string errMsg = ex.Message; if (display is IServiceMap && ((IServiceMap)display).MapServer != null) { IMapServer mapServer = ((IServiceMap)display).MapServer; await mapServer.LogAsync( ((IServiceMap)display).Name, "RenderRasterLayerThread", loggingMethod.error, ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace + "\n" + "filename=" + _filename + "\n" + "x=" + x.ToString() + "\n" + "y=" + y.ToString() + "\n" + "iWidth=" + iWidth.ToString() + "\n" + "iHeight=" + iHeight.ToString() + "\n" + "mag=" + mag.ToString() + "\n"); } else { throw ex; } return(null); } finally { if (bitmapData != null) { bitmap.UnlockBitmapPixelData(bitmapData); } MrSidWrapper.ReleaseBandData(bufferData); ReleaseReader(); } }