在C#中保存为JPEG从MemoryStream的 [英] Saving as jpeg from memorystream in c#
本文介绍了在C#中保存为JPEG从MemoryStream的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法如下图所示,以图像保存为JPEG格式。
我要救都具有相同的高度和宽度,没有它越来越扭曲的图片。
我怎样才能做到这一点?
请帮忙
公共无效SaveFileOnDisk(MemoryStream的MS,字符串文件名)
{
试
{
串APPPATH = HttpContext.Current.Request.ApplicationPath;
串physicalPath = HttpContext.Current.Request.MapPath(APPPATH);
串strPath的= physicalPath +\\Images
字符串工作目录= strPath的;
为System.Drawing.Image imgSave = System.Drawing.Image.FromStream(毫秒);
位图bmSave =新位图(imgSave);
位图bmTemp =新位图(bmSave);
图形grSave = Graphics.FromImage(bmTemp);
grSave.DrawImage(imgSave,0,0,imgSave.Width,imgSave.Height);
bmTemp.Save(工作目录+\\+文件名+.JPG);
imgSave.Dispose();
bmSave.Dispose();
bmTemp.Dispose();
grSave.Dispose();
}
赶上(异常前)
{
//lblMsg.Text =请稍后再试。
}
}
解决方案
调整图像并保存
私人无效ResizeImage(图片IMG,双了maxWidth,双了maxHeight)
{
双击srcWidth = img.Source.Width;
双srcHeight = img.Source.Height;
双resizeWidth = srcWidth;
双resizeHeight = srcHeight;
双方面= resizeWidth / resizeHeight;
如果(resizeWidth>了maxWidth)
{
resizeWidth =了maxWidth;
resizeHeight = resizeWidth /方面;
}
如果(resizeHeight>了maxHeight)
{
=方面resizeWidth / resizeHeight;
resizeHeight =了maxHeight;
resizeWidth = resizeHeight *方面;
}
img.Width = resizeWidth;
img.Height = resizeHeight;
}
您可以使用此代码来调整图像到所需博维保存前
I have a method as shown below to save image as jpeg. I want to save all the pictures with the same height and width without it getting distorted.
How can I do that? Please help
public void SaveFileOnDisk(MemoryStream ms, string FileName)
{
try
{
string appPath = HttpContext.Current.Request.ApplicationPath;
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
string strpath = physicalPath + "\\Images";
string WorkingDirectory = strpath;
System.Drawing.Image imgSave = System.Drawing.Image.FromStream(ms);
Bitmap bmSave = new Bitmap(imgSave);
Bitmap bmTemp = new Bitmap(bmSave);
Graphics grSave = Graphics.FromImage(bmTemp);
grSave.DrawImage(imgSave, 0, 0, imgSave.Width, imgSave.Height);
bmTemp.Save(WorkingDirectory + "\\" + FileName + ".jpg");
imgSave.Dispose();
bmSave.Dispose();
bmTemp.Dispose();
grSave.Dispose();
}
catch (Exception ex)
{
//lblMsg.Text = "Please try again later.";
}
}
解决方案
Resize the Image and Save it
Private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
double srcWidth = img.Source.Width;
double srcHeight = img.Source.Height;
double resizeWidth = srcWidth;
double resizeHeight = srcHeight;
double aspect = resizeWidth / resizeHeight;
if (resizeWidth > maxWidth)
{
resizeWidth = maxWidth;
resizeHeight = resizeWidth / aspect;
}
if (resizeHeight > maxHeight)
{
aspect = resizeWidth / resizeHeight;
resizeHeight = maxHeight;
resizeWidth = resizeHeight * aspect;
}
img.Width = resizeWidth;
img.Height = resizeHeight;
}
You could use this code to Resize the image to the required Dimention Before saving it
这篇关于在C#中保存为JPEG从MemoryStream的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文