调整图像大小并复制到文件夹 [英] Resizing an image and copying to the folder

查看:67
本文介绍了调整图像大小并复制到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码。问题是我得到的字节太大了。正在创建Jpeg文件,但它无法打开,因为它很大且已损坏。任何人都可以帮我修改这段代码



使用System;

使用System.IO;

使用System.Collections;

使用System.Drawing;

使用System.Drawing.Drawing2D;

使用System;

使用System.Text;

使用System.Threading.Tasks;



使用System.IO;

命名空间IrepAutomation

{

class program

{

static void Main(string [] args)

{



Image original = Image.FromFile(@C:\ Users \C180269 \Desktop\Source \ Presentation1.jpg );

图像已调整大小= ResizeImageForFull(原始,新大小(768,1024));



//将图像保存到内存stream

MemoryStream memStream = new MemoryStream();

resized.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg);


//使字节数组与图像大小相同

byte [] imageContent = new Byte [memStream.Length];



//回放内存流

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(图片图片,尺寸大小,bool preserveAspectRatio = true)

{

int newWidth = 0;

int newHeight = 0;



if(preserveAspectRatio)

{

newWidth = size.Width;

newHeight = size.Height;

}



图片newImage = new位图(newWidth,newHeight);

使用(Graphics graphicsHandle = Graphics.FromImage(newImage))

{

graphicsHandle.InterpolationMode = InterpolationMode。 HighQualityBicubic;

graphicsHandle.DrawImage(image,0,0,newWidth,newHeight);

}



返回newImage;

}





////处理传入目录中的所有文件,recurse在找到的任何目录

////上,处理它们包含的文件。

// public static void ProcessDirectory(string targetDirectory)

// {

// //处理目录中找到的文件列表。

// string [] fileEntries = Directory.GetFiles(targetDirectory);

// foreach(fileEntries中的字符串fileName)

// ProcessFile (fileName);



// //递归到这个目录的子目录中。

// string [] subirectoryEntries = Directory.GetDirectories(targetDirectory);

// foreach(子目录中的字符串子目录)

// ProcessDirectory (子目录);

//}



//在此插入处理找到文件的逻辑。

public static void ProcessFile(string path)

{

Console.WriteLine(Processed file' {0}'。,path) ;

Console.ReadKey();

}



}

}

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();
}

}
}

推荐答案

请查看我过去的详细答案:

在vb.net中调整图像大小 [ ^ ],

在流中读取Big Tiff和JPEG文件(>(23000 x 23000)pix)。并实时显示部分内容。 [ ^ ]。



请忽略其中一个问题标题中提到的VB.NET;答案与VB.NET无关,或适用于任何.NET语言。



-SA
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


这篇关于调整图像大小并复制到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆