我一直在努力将图像的缩略图添加到网格视图中,以便图像可以显示为原始图像的较小版本。
我仍然在学习C#编码,希望能得到一些帮助。我设法上传图片,甚至显示图片,但是将其设置为thumnail大小是有问题的。
我将插入一些代码作为众多尝试之一(所有这些都会返回错误)
亲切的问候
if (upImage.HasFile)
{
try
{
string filename = Path.GetFileName(upImage.FileName);
//****************************************************
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(Server.MapPath("~/") + filename);
System.Drawing.Image thumbNail = sourceImage.GetThumbnailImage(70, 70, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
thumbNail.Save(Path.ChangeExtension(filename, "thumb"));
upImage.SaveAs(Server.MapPath("~/") + filename);
//upImage.SaveAs(Server.MapPath("~/") + filename);
lblUpload.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
lblUpload.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
public bool ThumbnailCallback()
{
return true;
}
答案 0 :(得分:1)
public void Thumb(string file)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(70, 70, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int)imageStream.Length);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);
}
使用此功能,您将获得图像的缩略图,您可以在任何地方使用它。希望这有助于:)
答案 1 :(得分:0)
这是简短的回答:
private static MemoryStream GetImageResized(MemoryStream data, int previewWidth, int previewHeight)
{
const int pixelPadding = 6;
const int bottomSize = 0;
using (var src = new Bitmap(data))
{
// default to width-based resizing...
int width = previewWidth;
var height = (int)(previewWidth / (src.Width / (double)src.Height));
if (src.Width <= previewWidth && src.Height <= previewHeight)
{
// no resizing necessary...
width = src.Width;
height = src.Height;
}
else if (height > previewHeight)
{
// aspect is based on the height, not the width...
width = previewHeight / ((src.Height / src.Width) == 0 ? 1 : (src.Height / src.Width));
height = previewHeight;
}
using (
var dst = new Bitmap(width + pixelPadding, height + bottomSize + pixelPadding, PixelFormat.Format24bppRgb))
{
var rSrcImg = new Rectangle(0,0, src.Width, src.Height);
var rDstImg = new Rectangle(pixelPadding / 2, pixelPadding/2, dst.Width - pixelPadding, dst.Height - pixelPadding - bottomSize);
using (Graphics g = Graphics.FromImage(dst))
{
g.Clear(Color.FromArgb(64, 64, 64));
g.FillRectangle(Brushes.White, rDstImg);
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
g.DrawImage(src, rDstImg, rSrcImg, GraphicsUnit.Pixel);
}
var ms = new MemoryStream();
// save the bitmap to the stream...
dst.Save(ms, ImageFormat.Png);
ms.Position = 0;
return ms;
}
}
}
您可以调用此方法来调整图像大小。例如:
private void GetThumbnailImage(HttpContext context, string fileName, int maxWidth, int maxHeight)
{
var data = new MemoryStream();
using (var input = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var buffer = new byte[input.Length];
input.Read(buffer, 0, buffer.Length);
data.Write(buffer, 0, buffer.Length);
input.Close();
}
// reset position...
data.Position = 0;
MemoryStream ms = GetImageResized(
data, maxWidth, maxHeight);
context.Response.ContentType = "image/png";
// output stream...
context.Response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
// Possible Caching
//context.Response.Cache.SetCacheability(HttpCacheability.Public);
//context.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(2));
//context.Response.Cache.SetETag(...);
data.Dispose();
ms.Dispose();
}
我写了一篇关于它的文章。代码示例已附在文章中。这篇文章是波斯文,但你可以使用谷歌翻译。 http://www.devzone.ir/post/1389/09/16/Dynamic-Thumbnails-in-ASP.aspx