System.Drawing.Image.Save 함수를 호출 할 때 잘못된 매개 변수 오류가 발생합니다. Google은 몇 가지 제안을 찾았지만 아무 것도 작동하지 않습니다. 내가 뭘 하려는지, 내가 이미지를 업로드하고 100kb보다 큰 경우 이미지 크기를 절반으로 줄이고 싶습니다. 도와주세요.System.Drawing.Image.Save를 호출 할 때 잘못된 매개 변수 오류가 발생합니다.
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(realpath);
FullsizeImage = System.Drawing.Image.FromFile(realpath);
int fileSize = (int)new System.IO.FileInfo(realpath).Length;
while (fileSize > 100000) //If Larger than 100KB
{
SaveJpeg(realpath, FullsizeImage);
fileSize = (int)new System.IO.FileInfo(realpath).Length;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}
public static void SaveJpeg(string path, Image img)
{
Image NewImage = img;
img.Dispose();
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 85L);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
ImageCodecInfo jpegCodec = GetEncoderInfo(GetMimeType(path.Substring(path.LastIndexOf('.'), path.Length - path.LastIndexOf('.'))));
//THE ERROR IS HERE!!!!!!
NewImage.Save(path, jpegCodec, encoderParams);
//THE ERROR IS HERE!!!!!!
}
public static string GetMimeType(string extension)
{
if (extension == null)
{
throw new ArgumentNullException("extension");
}
if (!extension.StartsWith("."))
{
extension = "." + extension;
}
switch (extension.ToLower())
{
#region Big freaking list of mime types
// combination of values from Windows 7 Registry and
// from C:\Windows\System32\inetsrv\config\applicationHost.config
// some added, including .7z and .dat
case ".323": return "text/h323";
// more extension here..
#endregion
default:
// if you have logging, log: "No mime type is registered for extension: " + extension);
return "application/octet-stream";
}
}
편집 : 아래 코드를 수정하여 이미지가 예외없이 저장됩니다. 감사! 그러나 또 다른 문제는 여기에있다. 파일 크기가 줄어들지 않습니다. 내 while 루프는 절대 종료 할 수 없다는 것을 의미합니다. 도와 주시고 다시 감사드립니다. 당신이 image
객체를 폐기하고 있기 때문에
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(realpath)))
{
using (Image FullsizeImage = Image.FromStream(ms))
{
//code here
int fileSize = (int)new System.IO.FileInfo(realpath).Length;
while (fileSize > 100000) //If Larger than 100KB
{
SaveJpeg(realpath, FullsizeImage, 85L);
fileSize = (int)new System.IO.FileInfo(realpath).Length;
}
}
}
누군가가 제발 도와 드릴까요 내 문제가 아직 해결되지 :(
안녕하세요 AVD, 답변 해 주셔서 감사합니다. 하지만 그 줄을 제거하면이 오류가 발생합니다. 해당 파일이 현재 열려 있기 때문에 GDI + – user1643378
에서 일반 오류가 발생했습니다. – adatapost
나를 보내고 프로그램을 수정하자, 나는 여기에 최대한 빨리 업데이트를 게시 할 것이다. 감사. – user1643378