void AsyncDraw(object sender, DoWorkEventArgs e) { stopwatch = Stopwatch.StartNew(); renderer.Rotation = previewRotation; if (bwRenderer.CancellationPending) { return; } IsoCatResult result = renderer.Draw(map); if (result.Cancelled || bwRenderer.CancellationPending) { return; } Bitmap rawImage = result.Bitmap; if (rawImage != null) { previewImage = rawImage.Clone(result.CropRectangle, rawImage.PixelFormat); } GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized); }
void AsyncDraw(object sender, DoWorkEventArgs e) { stopwatch = Stopwatch.StartNew(); renderer.Rotation = previewRotation; if (bwRenderer.CancellationPending) { return; } renderer.ProgressChanged += (progressSender, progressArgs) => bwRenderer.ReportProgress(progressArgs.ProgressPercentage, progressArgs.UserState); IsoCatResult result = renderer.Draw(map); if (result.Canceled || bwRenderer.CancellationPending) { return; } Bitmap rawImage = result.Bitmap; if (rawImage != null) { previewImage = rawImage.Clone(result.CropRectangle, rawImage.PixelFormat); } GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized); }
void RenderLoop() { renderer = MakeRenderer(); using (MemoryStream ms = new MemoryStream()) { // loop terminates with the rest of the program (this is a background thread) while (true) { // wait (block) until a map is available for drawing RenderTask task = inQueue.WaitDequeue(); try { // render the map IsoCatResult result = renderer.Draw(task.Map); task.Map = null; // crop image (if needed) Image image; if (p.Uncropped) { image = result.Bitmap; } else { image = result.Bitmap.Clone(result.CropRectangle, result.Bitmap.PixelFormat); result.Bitmap.Dispose(); } // encode image if (p.ExportFormat.Equals(ImageFormat.Jpeg)) { EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, p.JpegQuality); image.Save(ms, p.ImageEncoder, encoderParams); } else if (p.ExportFormat.Equals(ImageFormat.Gif)) { OctreeQuantizer q = new OctreeQuantizer(255, 8); image = q.Quantize(image); image.Save(ms, p.ExportFormat); } else { image.Save(ms, p.ExportFormat); } image.Dispose(); // store result as a byte[] task.Result = ms.ToArray(); } catch (Exception ex) { task.Exception = ex; } // send stack to the results queue outQueue.Enqueue(task); ms.SetLength(0); } } }
void AsyncDraw(object sender, DoWorkEventArgs e) { stopwatch = Stopwatch.StartNew(); renderer = new IsoCat(map, IsoCatMode.Normal, previewRotation); Rectangle cropRectangle; if (bwRenderer.CancellationPending) { return; } Bitmap rawImage = renderer.Draw(out cropRectangle, bwRenderer); if (bwRenderer.CancellationPending) { return; } if (rawImage != null) { previewImage = rawImage.Clone(cropRectangle, rawImage.PixelFormat); } renderer = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized); }
static bool RenderOneMap([NotNull] FileSystemInfo fileSystemInfo) { if (fileSystemInfo == null) { throw new ArgumentNullException("fileSystemInfo"); } try { Map map; if (mapImporter != null) { if (!mapImporter.ClaimsName(fileSystemInfo.FullName)) { return(false); } Console.Write("Loading {0}... ", fileSystemInfo.Name); map = mapImporter.Load(fileSystemInfo.FullName); } else { Console.Write("Checking {0}... ", fileSystemInfo.Name); map = MapUtility.Load(fileSystemInfo.FullName); } string targetFileName; if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { targetFileName = fileSystemInfo.Name + imageFileExtension; } else { targetFileName = Path.GetFileNameWithoutExtension(fileSystemInfo.Name) + imageFileExtension; } string targetPath = Path.Combine(outputDirName, targetFileName); if (!overwrite && File.Exists(targetPath)) { Console.WriteLine(); if (!ShowYesNo("File \"{0}\" already exists. Overwrite?", targetFileName)) { return(false); } } Console.Write("Drawing... "); IsoCatResult result = renderer.Draw(map); Console.Write("Saving {0}... ", Path.GetFileName(targetFileName)); if (uncropped) { SaveImage(result.Bitmap, targetPath); } else { SaveImage(result.Bitmap.Clone(result.CropRectangle, result.Bitmap.PixelFormat), targetPath); } Console.WriteLine("ok"); return(true); } catch (NoMapConverterFoundException) { Console.WriteLine("skip"); return(false); } catch (Exception ex) { Console.WriteLine("ERROR"); Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message); return(false); } }
public void MapViewerUpdateLevel() { try { Level l = Level.Find(txtMapViewerLevelName.Text); if (l == null) { Command.all.Find("load").Use(null, txtMapViewerLevelName.Text); l = Level.Find(txtMapViewerLevelName.Text); } if (l == null) { MessageBox.Show("Level could not be found.", "Map Viewer"); return; } txtMapViewerX.Text = l.width.ToString(); txtMapViewerY.Text = l.depth.ToString(); txtMapViewerZ.Text = l.height.ToString(); int rotation = (int)txtMapViewerRotation.Value; if (rotation < 0 || rotation > 3) { MessageBox.Show("Invalid rotation (must be from 0-3).", "Map Viewer"); return; } IsoCat IsoCat = new IsoCat(l, IsoCatMode.Normal, rotation); Rectangle r = new Rectangle(0, 0, picMapViewer.Width, picMapViewer.Height); System.ComponentModel.BackgroundWorker bgw = new System.ComponentModel.BackgroundWorker(); bgw.WorkerReportsProgress = true; MapViewerLastDrawn = IsoCat.Draw(out r, bgw); picMapViewer.Image = MapViewerLastDrawn; } catch (Exception ex) { Server.ErrorLog(ex); } }
static void RenderOneMap([NotNull] FileSystemInfo fileSystemInfo, [NotNull] string relativeName) { if (fileSystemInfo == null) { throw new ArgumentNullException("fileSystemInfo"); } if (relativeName == null) { throw new ArgumentNullException("relativeName"); } try { // if output directory was not given, save to same directory as the mapfile if (!outputDirGiven) { outputDirName = Paths.GetDirectoryNameOrRoot(fileSystemInfo.FullName); } // load the mapfile Map map; if (mapImporter != null) { if (!mapImporter.ClaimsName(fileSystemInfo.FullName)) { return; } Console.Write("Loading {0}... ", relativeName); map = mapImporter.Load(fileSystemInfo.FullName); } else { Console.Write("Checking {0}... ", relativeName); map = MapUtility.Load(fileSystemInfo.FullName, tryHard); } // select target image file name string targetFileName; if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { targetFileName = fileSystemInfo.Name + imageFileExtension; } else { targetFileName = Path.GetFileNameWithoutExtension(fileSystemInfo.Name) + imageFileExtension; } // get full target image file name, check if it already exists string targetPath = Path.Combine(outputDirName, targetFileName); if (!overwrite && File.Exists(targetPath)) { Console.WriteLine(); if (!ShowYesNo("File \"{0}\" already exists. Overwrite?", targetFileName)) { return; } } // draw and save Console.Write("Drawing... "); IsoCatResult result = renderer.Draw(map); Console.Write("Saving {0}... ", Path.GetFileName(targetFileName)); if (uncropped) { SaveImage(result.Bitmap, targetPath); } else { SaveImage(result.Bitmap.Clone(result.CropRectangle, result.Bitmap.PixelFormat), targetPath); } Console.WriteLine("ok"); } catch (NoMapConverterFoundException) { Console.WriteLine("skip"); } catch (Exception ex) { Console.WriteLine("ERROR"); Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex); } }