I have the code like this. Issue is the Bytes I am getting is too large. The Jpeg file is being created but its not able to opensince it is large and corrupt. can any one help me on this code correction
using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace IrepAutomation
{
class Program
{
static void Main(string[] args)
{
Image original = Image.FromFile(@"C:\Users\C180269\Desktop\Source\Presentation1.jpg");
Image resized = ResizeImageForFull(original, new Size(768, 1024));
//save the image to memory stream
MemoryStream memStream = new MemoryStream();
resized.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// make byte array the same size as the image
byte[] imageContent = new Byte[memStream.Length];
// rewind the memory stream
memStream.Position = 0;
memStream.Read(imageContent, 0,(int)memStream.Length);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
using (StreamWriter sw = new StreamWriter(Path.Combine(desktop, "actualBytes.jpeg")))
sw.Write(String.Join(Environment.NewLine, imageContent));
}
public static Image ResizeImageForFull(Image image, Size size, bool preserveAspectRatio = true)
{
int newWidth=0;
int newHeight=0;
if (preserveAspectRatio)
{
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
//// Process all files in the directory passed in, recurse on any directories
//// that are found, and process the files they contain.
//public static void ProcessDirectory(string targetDirectory)
//{
// // Process the list of files found in the directory.
// string[] fileEntries = Directory.GetFiles(targetDirectory);
// foreach (string fileName in fileEntries)
// ProcessFile(fileName);
// // Recurse into subdirectories of this directory.
// string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
// foreach (string subdirectory in subdirectoryEntries)
// ProcessDirectory(subdirectory);
//}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
Console.ReadKey();
}
}
}