private SystemBitmap FindBestMatch(SystemBitmap[] imagesFromLargestToSmallest, int desiredSize) { if (imagesFromLargestToSmallest.Length == 0) { return(null); } SystemBitmap best = imagesFromLargestToSmallest[0]; if (imagesFromLargestToSmallest.Length == 1) { return(best); } for (int i = 1; i < imagesFromLargestToSmallest.Length; ++i) { SystemBitmap current = imagesFromLargestToSmallest[i]; if (current.Width == desiredSize) { return(current); } if (current.Width < desiredSize) { return(best); } best = current; } return(best); }
private void ExportImageFile(string path, SystemBitmap image) { using (new PerformanceSection("ExportImageFile")) { image.Save(path); } }
public void Blit(SystemBitmap bmp, int x, int y) { #if WINDOWS this.systemGraphics.DrawImageUnscaled(bmp.bitmap, x, y); #elif OSX this.context.SetSource(bmp.bitmap, x, y); this.context.Paint(); #endif }
public void Blit(SystemBitmap bmp, int x, int y, int stretchWidth, int stretchHeight) { #if WINDOWS this.systemGraphics.DrawImage(bmp.bitmap, x, y, stretchWidth, stretchHeight); #elif OSX this.context.Scale(1.0 * this.sysBmp.Width / bmp.bitmap.Width, 1.0 * this.sysBmp.Height / bmp.bitmap.Height); this.context.SetSource(bmp.bitmap, x, y); this.context.Paint(); #endif }
public Graphics(SystemBitmap owner) { #if WINDOWS this.systemGraphics = System.Drawing.Graphics.FromImage(owner.bitmap); #elif OSX this.sysBmp = owner; this.context = new Cairo.Context(owner.bitmap); #endif undisposed.Add(this); }
public Dictionary <int, SystemBitmap> GenerateWithDefaultFallback() { if (this.bitmaps.Count == 0) { SystemBitmap defaultIcon = new SystemBitmap(typeof(Util).Assembly, "icons/crayon_logo.png"); this.ownedBitmapReferences.Add(defaultIcon); this.AddInputImage(defaultIcon); } return(Generate()); }
public byte[] GenerateIconFile() { int[] sizes = this.bitmaps.Keys.OrderBy(i => i).ToArray(); if (sizes.Length == 0) { throw new InvalidOperationException("Cannot generate empty icon file."); } List <byte> imageBytes = new List <byte>(); List <int> startingPositions = new List <int>(); int startingPosition = 6 + 16 * sizes.Length; // ico header is 6 bytes and each file header is 16 bytes. List <byte> icoHeader = new List <byte>(); List <byte> pngHeaders = new List <byte>(); List <byte> pngPayloads = new List <byte>(); ToLittleEndian(0, 2, icoHeader); // first two bytes are always 0 ToLittleEndian(1, 2, icoHeader); // 1 for ICO format (2 is CUR) ToLittleEndian(sizes.Length, 2, icoHeader); // number of files foreach (int size in sizes) { SystemBitmap originalImage = this.bitmaps[size]; int width = originalImage.Width; int height = originalImage.Height; int x = (size - width) / 2; int y = (size - height) / 2; SystemBitmap resource = new SystemBitmap(size, size); SystemBitmap.Graphics g = resource.MakeGraphics(); g.Blit(originalImage, x, y); g.Cleanup(); byte[] pngBytes = resource.SaveBytes(ImageFormat.PNG); pngPayloads.AddRange(pngBytes); ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders); ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders); ToLittleEndian(0, 1, pngHeaders); // 0 for not using a color palette ToLittleEndian(0, 1, pngHeaders); // reserved, always 0 ToLittleEndian(0, 2, pngHeaders); // 0 color planes. ToLittleEndian(32, 2, pngHeaders); // 32 bits per pixel ToLittleEndian(pngBytes.Length, 4, pngHeaders); // file size in bytes ToLittleEndian(startingPosition, 4, pngHeaders); // byte position from the beginning of the file startingPosition += pngBytes.Length; } List <byte> finalOutput = icoHeader; finalOutput.AddRange(pngHeaders); finalOutput.AddRange(pngPayloads); return(finalOutput.ToArray()); }
public SystemBitmap CloneToNewSize(int width, int height) { SystemBitmap newBitmap = new SystemBitmap(width, height); Graphics g = newBitmap.MakeGraphics(); if (width == this.Width && height == this.Height) { g.Blit(this, 0, 0); } else { g.Blit(this, 0, 0, width, height); } g.Cleanup(); return(newBitmap); }
public IconSetGenerator AddInputImage(SystemBitmap bmp) { if (bmp.Width != bmp.Height) { int size = Math.Max(bmp.Width, bmp.Height); SystemBitmap newBmp = new SystemBitmap(size, size); this.ownedBitmapReferences.Add(newBmp); SystemBitmap.Graphics g = newBmp.MakeGraphics(); int x = (size - bmp.Width) / 2; int y = (size - bmp.Height) / 2; g.Blit(bmp, x, y); g.Cleanup(); bmp = newBmp; } this.bitmaps.Add(bmp); this.bitmapMaxDimenion.Add(Math.Max(bmp.Width, bmp.Height)); return(this); }
public void AddImage(SystemBitmap bmp) { int width = bmp.Width; int height = bmp.Height; int largest = System.Math.Max(width, height); if (width > 256 || height > 256) { throw new InvalidOperationException("Icon images cannot be larger than 256 x 256"); } foreach (int size in new int[] { 256, 128, 64, 32 }) { if (largest > size / 2) { this.bitmaps[size] = bmp; return; } } this.bitmaps[16] = bmp; }
public Dictionary <int, SystemBitmap> Generate() { Dictionary <int, SystemBitmap> lookup = new Dictionary <int, SystemBitmap>(); SystemBitmap[] sources = this.bitmaps.OrderBy(b => - b.Width).ToArray(); foreach (int desiredSize in this.outputSizes) { SystemBitmap source = this.FindBestMatch(sources, desiredSize); SystemBitmap bmp = new SystemBitmap(desiredSize, desiredSize); SystemBitmap.Graphics g = bmp.MakeGraphics(); g.Blit(source, 0, 0, desiredSize, desiredSize); g.Cleanup(); lookup.Add(desiredSize, bmp); } this.Cleanup(); return(lookup); }
public static void WriteFileImage(string path, SystemBitmap image) { image.Save(path); }