입력 스트림 (이미지 압축 파일)을 가져 와서 각 파일을 추출하려고합니다. 하지만 저장하기 전에 각 이미지의 품질을 낮춰야합니다 (품질이 < 인 경우 100). 다음을 시도했지만 이미지를 압축하지 않습니다.저장하기 전에 이미지/스트림 품질 줄이기
public void UnZip(Stream inputStream, string destinationPath, int quality = 80) {
using (var zipStream = new ZipInputStream(inputStream)) {
ZipEntry entry;
while ((entry = zipStream.GetNextEntry()) != null) {
var directoryPath = Path.GetDirectoryName(destinationPath + Path.DirectorySeparatorChar + entry.Name);
var fullPath = directoryPath + Path.DirectorySeparatorChar + Path.GetFileName(entry.Name);
// Create the stream to unzip the file to
using (var stream = new MemoryStream()) {
// Write the zip stream to the stream
if (entry.Size != 0) {
var size = 2048;
var data = new byte[2048];
while (true) {
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
stream.Write(data, 0, size);
else
break;
}
}
// Compress the image and save it to the stream
if (quality < 100)
using (var image = Image.FromStream(stream)) {
var info = ImageCodecInfo.GetImageEncoders();
var @params = new EncoderParameters(1);
@params.Param[0] = new EncoderParameter(Encoder.Quality, quality);
image.Save(stream, info[1], @params);
}
}
// Save the stream to disk
using (var fs = new FileStream(fullPath, FileMode.Create)) {
stream.WriteTo(fs);
}
}
}
}
}
누군가 내가 잘못하고있는 것을 보여줄 수 있으면 감사하겠습니다. 또한 코드 정리에 대한 조언은보기 흉하게 성장했습니다. 감사합니다
당신과 당신의 코드는 모순됩니다. "압축 해제"는 압축 해제를 의미하며 압축하는 이유에 대한 질문입니다. 당신의 질문에 더 분명하게 될 수 있습니까? –
Peter 안녕하세요, 저는 zip 파일의 압축을 풀고 결과 파일을 개별적으로 압축해야했습니다. 이렇게하면 디스크 공간을 절약 할 수 있습니다. 나는 스콧의 대답이 도움이 될 것이라고 생각한다. – nfplee