调整/重命名上传图像 [英] Resize / Rename upload image
问题描述
大家好,
我正在尝试为我的网站创建一个控件,用户可以通过javascript代码上传图片需要具有特定名称和特定大小width / heigth的图像,
所以在我的C#文件中我创建以下代码以便make it但它没有用,
重命名代码t工作正常,问题是图像的大小调整它不起作用(我在codeproject中发现了一个相关文章,调整大小图像但是不幸的是,它使用的是Mvc技术而我不这样做。
任何人都可以帮助我修改我的代码中的内容,以便修复将上传的图像的大小调整吗?
Thnx提前!!!
Hi guys,
I' m trying to create a control for my web site where the user will be able to upload images, due to a javascript code i need the images with specific names and specific size "width/heigth",
So in my C# file I create the following code in order to make it but it didn't work,
the rename code t works fine , the issue is the resize of the image which it didn't work (I found a relative article in codeproject with resize images but unfortunatelly it uses Mvc tech and I don't).
Could anyone help me what I should modify in my code in order to fix the resizing of the image which will be upload it?
Thnx in advance!!!
string subimages2 = "~/gallery/thumbs/";
Boolean fileOK2 = false;
String path2 = Server.MapPath(subimages2);
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK2 = true;
}
}
}
if (fileOK2)
{
try
{
int count = 10;
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string fileExtension = Path.GetExtension(FileUpload1.FileName);
fileNameWithoutExtension = Convert.ToString(count);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/gallery/thumbs/" + fileNameWithoutExtension + fileExtension));
//modify width heigth
HttpPostedFile pf = FileUpload1.PostedFile;
System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream);
bm = ResizeBitmap((Bitmap)bm, 150, 150); /// new width, height
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
return result;
}
推荐答案
参考
http://imageresizing.net/docs/ howto / upload-and-resize [ ^ ]
http://stackoverflow.com/questions/254419/asp-net-image -uploading-with-resizing [ ^ ]
Refer
http://imageresizing.net/docs/howto/upload-and-resize[^]
http://stackoverflow.com/questions/254419/asp-net-image-uploading-with-resizing[^]
public static Bitmap GetThumbnail(Bitmap source, int maxWidth, int maxHeight)
{
int iHeight = 0;
int iWidth = 0;
iHeight = source.Height;
iWidth = source.Width;
if (source.Width > maxWidth)
{
if (source.Width > source.Height)
{
iWidth = maxWidth;
iHeight = source.Height * maxWidth / source.Width;
}
else
{
iHeight = maxHeight;
iWidth = source.Width * maxHeight / source.Height;
}
}
else if (source.Height > maxHeight)
{
if (source.Width > source.Height)
{
iWidth = maxWidth;
iHeight = source.Height * maxWidth / source.Width;
}
else
{
iHeight = maxHeight;
iWidth = source.Width * maxHeight / source.Height;
}
}
Bitmap dest = new Bitmap(source, iWidth, iHeight);
return dest;
}
这篇关于调整/重命名上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!