Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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();
}

}
}
Posted

1 solution

Please see my past detailed answers:
resize image in vb.net[^],
Read Big Tiff and JPEG files (>(23000 x 23000) pix) in a stream. And display part of it to the screen in realtime.[^].

Please ignore "VB.NET" mentioned in one of the question titles; the answers have nothing to do with VB.NET, or are applicable to any .NET language.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900