사용자가 이미지를 업로드 할 수 있지만 이미지를 업로드 할 수있게 해주는 fileupload 컨트롤이 있습니다. 이미지를 크기가 640x480 크기로 변경하려는 경우 다음에 수행 할 작업을 파악할 수 없습니다. 이것이 내가 가진 것입니다.파일에 스트림 저장
// CALL THE FUNCTION THAT WILL RESIZE THE IMAGE
protected void btnUploadFile_Click(object sender, EventArgs e)
{
Stream imgStream = ir.ResizeFromStream(640, fupItemImage.PostedFile.InputStream);
// What to do next?
}
// THE FUNCTION THAT WILL RESIZE IMAGE THEN RETURN AS MEMORY STREAM
public MemoryStream ResizeFromStream(int MaxSideSize, Stream Buffer)
{
int intNewWidth;
int intNewHeight;
System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);
// GET IMAGE FORMAT
ImageFormat fmtImageFormat = imgInput.RawFormat;
// GET ORIGINAL WIDTH AND HEIGHT
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;
// IS LANDSCAPE OR PORTRAIT ??
int intMaxSide;
if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
// SET NEW WIDTH AND HEIGHT
double dblCoef = MaxSideSize/(double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
// CREATE NEW BITMAP
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
// SAVE BITMAP TO STREAM
MemoryStream imgMStream = new MemoryStream();
bmpResized.Save(imgMStream, imgInput.RawFormat);
// RELEASE RESOURCES
imgInput.Dispose();
bmpResized.Dispose();
Buffer.Close();
return imgMStream;
}
감사합니다
'imgStream.Position = 0'을 설정하여'imgStream'의 위치를 먼저 되 감을 필요가 있습니다. –
FWIW, bytesRead! = length이지만 bytesRead! = 0은 완벽하게 유효하며 스트림 imgStream은 비어 있지 않습니다. NetworkStream. 'while (bytesRead! = 0)'이 더 안전합니다. –
(imgStream은이 경우 메모리 스트림입니다.하지만 하나의 스트림에서 다른 스트림으로 복사 할 때 일반적으로 'Stream'의 의미 계약을 아는 것이 좋습니다.) –