Jump to content

Save as Base64


Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by jerone
Link to comment
Share on other sites

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

PdnSig.png

Plugin Pack | PSFilterPdn | Content Aware Fill | G'MICPaint 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

 

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 by jerone
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...