0
.NET을 사용하여 이미지를 만들려고합니다. 아래는 내가 사용하고있는 코드입니다. 이것은 대부분 잘 작동하지만 때로는 스트림이 끊어지고 파일이 손상되었다고 추측합니다. URL을 통해 이미지를 가져 오는 중입니다..NET에서 손상된 파일을 만들었습니다.
나는 정말로 내 코드를 수정하거나 내가 할 수있는 것에 대한 통찰력을 제공 할 사람을 찾고있다. 나는 다음과 같은 오류를 받고 있어요
감사
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;
// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
:
Main Exception
MESSAGE: Parameter is not valid.
SOURCE: System.Drawing
TARGETSITE: System.Drawing.Image FromStream(System.IO.Stream, Boolean, Boolean)
STACKTRACE: at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at Sitecore.Resources.Media.ImageMedia.GetImage() at Sitecore.Resources.Media.ImageMedia.UpdateMetaData(MediaStream mediaStream)
at Sitecore.Resources.Media.JpegMedia.UpdateMetaData(MediaStream mediaStream) at Sitecore.Resources.Media.MediaCreator.AttachStreamToMediaItem(Stream stream, String itemPath, String fileName, MediaCreatorOptions options)
at Sitecore.Resources.Media.MediaCreator.CreateFromStream(Stream stream, String filePath, MediaCreatorOptions options)
at Sitecore.Resources.Media.MediaCreator.CreateFromFile(String filePath, MediaCreatorOptions options)
예외가 잡히지 않았습니까 ?? 그리고 그들은 무엇을 말합니까 ?? – PaulG
왜 예외를 잡아서 다시 던지려하지만 스택 추적을 잃어 버리는거야? 그리고 왜'using '문장을 사용하지 않습니까? –
가끔 중단 된 TCP 연결은 오류를주지 않지만 스트림 끝에 도달 한 것처럼 동작합니다. C#에서 다운로더를 작성할 때 임시 파일로 다운로드 한 다음 예상 된 크기 (http 헤더의)가 다운로드 한 크기와 일치하는지 확인한 후에 만 대상 파일 이름으로 이동했습니다. – CodesInChaos