public unsafe void Extract(out IntPtr phBmpThumbnail) { System.IO.StreamWriter logfile = new System.IO.StreamWriter("C:\thumbs-log.txt", true); logfile.WriteLine("Called Extract"); logfile.Close(); System.Drawing.Bitmap designBitmap = designFile.DesignToBitmap(3, false, 0, 1.0f); IntPtr hBmp = designBitmap.GetHbitmap(); phBmpThumbnail = new IntPtr(); // Assuming you already have hBmp as the handle to your Bitmap object... if (IntPtr.Size == 4) { int *pBuffer = (int *)phBmpThumbnail.ToPointer(); * pBuffer = hBmp.ToInt32(); // IMO, casting back to (void*) is not necessary. // I guess just for formality, that pBuffer points // to an object, not an int. :) phBmpThumbnail = new IntPtr((void *)pBuffer); } else // 8-bytes, or 64-bit { long *pBuffer = (long *)phBmpThumbnail.ToPointer(); * pBuffer = hBmp.ToInt64(); phBmpThumbnail = new IntPtr((void *)pBuffer); } }
static void GenerateImage(string filename) { try { PesFile.PesFile design = new PesFile.PesFile(filename); Bitmap DrawArea = design.DesignToBitmap(5.0f, false, 0.0f, 1.0f); Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); using (Graphics tempGraph = Graphics.FromImage(temp)) { tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height); tempGraph.DrawImageUnscaled(DrawArea, 0, 0); tempGraph.Dispose(); temp.Save(System.IO.Path.ChangeExtension(filename, ".png")); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private void UpdateDesignImage() { if (design == null) { // No design loaded - nothing to update return; } // Assume 96 DPI until we can come up with a better way to calculate it float screenDPI = 96; Bitmap tempImage = design.DesignToBitmap((float)settings.threadThickness, (settings.filterStiches), settings.filterStitchesThreshold, (screenDPI / design.NativeDPI)); // Rotate image switch (designRotation) { case 90: tempImage.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 180: tempImage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 270: tempImage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // Scale image if (fitToWindowToolStripMenuItem.Checked) { // Calculate size of image based on available drawing area float windowWidth = panel2.Width - 3; float windowHeight = panel2.Height - 3; // Figure out which dimension is more constrained float widthScale = windowWidth / tempImage.Width; float heightScale = windowHeight / tempImage.Height; if (widthScale < heightScale) { designScale = widthScale; } else { designScale = heightScale; } } int width = (int)(tempImage.Width * designScale); int height = (int)(tempImage.Height * designScale); if (width < 1 || height < 1) { // Image area is too small to update return; } if (width != tempImage.Width || height != tempImage.Height) { // Scale image code from http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp Rectangle destRect = new Rectangle(0, 0, width, height); Bitmap scaledImage = new Bitmap(width, height); scaledImage.SetResolution(tempImage.HorizontalResolution, tempImage.VerticalResolution); using (var graphics = Graphics.FromImage(scaledImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(tempImage, destRect, 0, 0, tempImage.Width, tempImage.Height, GraphicsUnit.Pixel, wrapMode); } } // Keep the scaled image and dispose the intermediate image tempImage.Dispose(); tempImage = scaledImage; } // About to abandon the current DrawArea object, dispose it now if (DrawArea != null) { DrawArea.Dispose(); DrawArea = null; } // Add transparency grid if (settings.transparencyGridEnabled) { DrawArea = new Bitmap(tempImage.Width, tempImage.Height); using (Graphics g = Graphics.FromImage(DrawArea)) { Color gridColor = settings.transparencyGridColor; using (Pen gridPen = new Pen(gridColor)) { int gridSize = settings.transparencyGridSize; for (int xStart = 0; (xStart * gridSize) < DrawArea.Width; xStart++) { for (int yStart = 0; (yStart * gridSize) < DrawArea.Height; yStart++) { // Fill even columns in even rows and odd columns in odd rows if ((xStart % 2) == (yStart % 2)) { g.FillRectangle(gridPen.Brush, (xStart * gridSize), (yStart * gridSize), gridSize, gridSize); } } } } g.DrawImage(tempImage, 0, 0); // Done with tempImage tempImage.Dispose(); tempImage = null; } } else { // Keeping the object tempImage was pointing at, so don't dispose it DrawArea = tempImage; } panel1.Width = DrawArea.Width; panel1.Height = DrawArea.Height; panel1.Invalidate(); panel2LastUpdateSize = panel2.Size; // Update window title this.Text = System.IO.Path.GetFileName(loadedFileName) + " (" + (designScale * 100).ToString("0") + "%) - " + APP_TITLE; }