From b7697a01f8c19446698e16edbfe7f4afec7eb5ae Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Thu, 14 Jan 2021 16:21:12 -0800 Subject: [PATCH] Add code to wrangle images in/out of the sql database. --- ImageWrangler.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ImageWrangler.cs diff --git a/ImageWrangler.cs b/ImageWrangler.cs new file mode 100644 index 0000000..841e923 --- /dev/null +++ b/ImageWrangler.cs @@ -0,0 +1,40 @@ +using System.Drawing; +using System.IO; + +namespace GreatHomeChildcare +{ + class ImageWrangler + { + /* Convert student photo to byte array for saving to db. + * Also used to see if the photo is too large for the db blob type. + * INPUT: Object + * OUPUT byte[] array + * https://stackoverflow.com/questions/3801275/how-to-convert-image-to-byte-array + */ + public byte[] ImageToByteArray(Image imageIn) + { + if (imageIn == null) + return null; + + using (var ms = new MemoryStream()) + { + imageIn.Save(ms, imageIn.RawFormat); + return ms.ToArray(); + } + } + + /* Convert student photo from byte array to object. + * Convert a byte array to an Object + * INPUT: byte[] array + * OUTPUT: Image + * https://stackoverflow.com/questions/9173904/byte-array-to-image-conversion + */ + public static Image ByteArrayToImage(byte[] arrBytes) + { + using (var ms = new MemoryStream(arrBytes)) + { + return Image.FromStream(ms); + } + } + } +}