MakeIco.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13.  
  14. namespace DsImagingWeb.Demos
  15. {
  16. // This sample loads a large PNG image, resizes it to several smaller sizes
  17. // commonly used in icons, and creates a multi-frame .ICO from those.
  18. // See also the ReadIco sample which renders the frames from the .ICO
  19. // created by this sample.
  20. public class MakeIco
  21. {
  22. public string DefaultMime { get => Common.Util.MimeTypes.ICO; }
  23.  
  24. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  25. {
  26. if (targetMime != Common.Util.MimeTypes.ICO)
  27. throw new Exception("This sample only supports ICO output format.");
  28.  
  29. var srcPath = Path.Combine("Resources", "ImagesBis", "mescius-logomark-c.png");
  30. var srcBmp = new GcBitmap();
  31.  
  32. using (var s = File.OpenRead(srcPath))
  33. srcBmp.Load(s);
  34.  
  35. using (var bmp16 = srcBmp.Resize(16, 16))
  36. using (var bmp24 = srcBmp.Resize(24, 24))
  37. using (var bmp32 = srcBmp.Resize(32, 32))
  38. using (var bmp48 = srcBmp.Resize(48, 48))
  39. using (var bmp256 = srcBmp.Resize(256, 256))
  40. using (var ico = new GcIco())
  41. {
  42. ico.Frames.Add(new IcoFrame(bmp256, IcoFrameEncoding.Png));
  43. ico.Frames.Add(new IcoFrame(bmp48, IcoFrameEncoding.Png));
  44. ico.Frames.Add(new IcoFrame(bmp32, IcoFrameEncoding.Png));
  45. ico.Frames.Add(new IcoFrame(bmp24, IcoFrameEncoding.Png));
  46. ico.Frames.Add(new IcoFrame(bmp16, IcoFrameEncoding.Png));
  47.  
  48. var ms = new MemoryStream();
  49. ico.Save(ms);
  50. ms.Seek(0, SeekOrigin.Begin);
  51. return ms;
  52. }
  53. }
  54. }
  55. }
  56.