Introduction
Base64 encoding is commonly used when there is a requirement to convert binary data to string format. Base64 is commonly used in a number of applications, including email via MIME, and storing complex data in XML.
Here in this article I have created a service which will first resize images then convert images in base64 strings and then will return base64 images in JSON format.
Let’s go thru the demonstration.
Create service
Service1.svc.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace PhotoService
- {
- public class Service1: IService1
- {
- static string[] ImageBase64Strings;
- static int ImageCounts;
- static string[] base64Value;
- DirectoryInfo di;
- System.Drawing.Image imgThumb = null;
- int thumbWidth = 100;
- int thumbHeight = 100;
- Service1() {
- di = new DirectoryInfo(@
- "D:\ImageFolder"); // Path of images
- ImageCounts = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly).Length;
- base64Value = new string[ImageCounts];
- }
- public string[] GetImages() //This method will return base64 strings in JSON format
- {
- try
- {
- int i = 0;
- FileInfo[] finfos = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
- ImageBase64Strings = new string[ImageCounts];
- foreach(FileInfo fi in finfos)
- {
- using(System.Drawing.Image img = System.Drawing.Image.FromFile(fi.FullName)) {
- imgThumb = CreateThumbnail(img, thumbWidth, thumbHeight);
- using(MemoryStream m = new MemoryStream())
- {
- imgThumb.Save(m, ImageFormat.Jpeg);
- byte[] imageBytes = m.ToArray();
- ImageBase64Strings[i] = Convert.ToBase64String(imageBytes);
- i++;
- }
- }
- }
- return ImageBase64Strings;
- } catch (Exception ex)
- {
- throw ex;
- }
- }
- private Image CreateThumbnail(Image image, int thumbWidth, int thumbHeight)
- {
- try
- {
- return image.GetThumbnailImage(
- thumbWidth,
- thumbHeight,
- delegate()
- {
- return false;
- },
- IntPtr.Zero);
- } catch (Exception ex)
- {
- throw ex;
- }
- }
- public Stream ToStream(Image image, ImageFormat formaw)
- {
- try
- {
- var stream = new System.IO.MemoryStream();
- image.Save(stream, formaw);
- stream.Position = 0;
- return stream;
- } catch (Exception ex)
- {
- throw;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace PhotoService
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebGet(UriTemplate = "/GetImages", RequestFormat = WebMessageFormat.Json,
- ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
- string[] GetImages();
- }
- }

Join the conversation! Your thoughts help the community grow.