jerone Posted July 13, 2013 Share Posted July 13, 2013 I was wondering if it's possible to save an image as Base64 string? Maybe as a plugin? Quote Link to comment Share on other sites More sharing options...
Ego Eram Reputo Posted July 13, 2013 Share Posted July 13, 2013 I'm not sure that it is exactly what you're looking for, but the plugin Image to Code pipes an image to a C data structure. Quote ebook: Mastering Paint.NET | resources: Plugin Index | Stereogram Tut | proud supporter of Codelab plugins: EER's Plugin Pack | Planetoid | StickMan | WhichSymbol+ | Dr Scott's Markup Renderer | CSV Filetype | dwarf horde plugins: Plugin Browser | ShapeMaker Link to comment Share on other sites More sharing options...
BoltBait Posted July 14, 2013 Share Posted July 14, 2013 I'm not sure that it is exactly what you're looking for, but the plugin Image to Code pipes an image to a C data structure.Totally not the same thing. It would be an interesting filetype plugin. I'm sure one of the filetype plugin authors could knocking something like this out in just a few minutes. Quote Download: BoltBait's Plugin Pack | CodeLab | and a Free Computer Dominos Game Link to comment Share on other sites More sharing options...
jerone Posted July 14, 2013 Author Share Posted July 14, 2013 (edited) It would be an interesting filetype plugin. I'm sure one of the filetype plugin authors could knocking something like this out in just a few minutes. So this is possible with plugins? Can you point me in the right direction for documentation about how I can make a save-as plugin, and I mite have a go myself... Edited July 14, 2013 by jerone Quote Link to comment Share on other sites More sharing options...
null54 Posted July 14, 2013 Share Posted July 14, 2013 It would be an interesting filetype plugin. I'm sure one of the filetype plugin authors could knocking something like this out in just a few minutes. Done: Base64 FileType Quote Plugin Pack | PSFilterPdn | Content Aware Fill | G'MIC | Paint Shop Pro Filetype | RAW Filetype | WebP Filetype The small increase in performance you get coding in C++ over C# is hardly enough to offset the headache of coding in the C++ language. ~BoltBait Link to comment Share on other sites More sharing options...
jerone Posted July 14, 2013 Author Share Posted July 14, 2013 (edited) Done: Base64 FileType I was just in the progress of building my own (see code below), but yours works great with the extra option for file type. using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Text; using System.Windows.Forms; namespace PaintDotNet.Plugin.FileTypes { /// <summary> /// Base64 file type /// </summary> public class Base64FileType : FileType { #region Constructor; /// <summary> /// Base64 file type /// </summary> public Base64FileType() : base("Base64", FileTypeFlags.SupportsLoading | FileTypeFlags.SupportsSaving, new String[] { ".txt" }) { } #endregion Constructor; #region Override; /// <summary> /// Open file /// </summary> /// <param name="input"></param> /// <returns></returns> protected override Document OnLoad(Stream input) { try { // Convert byte[] string to Base64; StreamReader reader = new StreamReader(input); String base64String = reader.ReadToEnd(); base64String = base64String.Replace("data:image/png;base64,", ""); // Convert Base64 string to byte[]; byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image; ms.Write(imageBytes, 0, imageBytes.Length); Image image = Image.FromStream(ms, true); return Document.FromImage(image); } catch { MessageBox.Show("Problem Importing File"); Bitmap b = new Bitmap(500, 500); return Document.FromImage(; } } /// <summary> /// Save file /// </summary> /// <param name="input"></param> /// <param name="output"></param> /// <param name="token"></param> /// <param name="scratchSurface"></param> /// <param name="callback"></param> protected override void OnSave(Document input, Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback) { RenderArgs ra = new RenderArgs(new Surface(input.Size)); input.Render(ra, true); using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[]; ra.Bitmap.Save(ms, ImageFormat.Gif); Byte[] imageBytes = ms.ToArray(); // Convert byte[] to Base64 string; String base64String = "data:image/png;base64," + Convert.ToBase64String(imageBytes); // Convert Base64 string to byte[]; ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] stringBytes = encoding.GetBytes(base64String); // Save to stream; output.Write(stringBytes, 0, stringBytes.Length); } } #endregion Override; } public class Base64FileTypeFactory : IFileTypeFactory { public FileType[] GetFileTypeInstances() { return new FileType[] { new Base64FileType() }; } } } Edited July 14, 2013 by jerone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.