我想从文件位置的gridview中显示缩略图图像。如何生成.jpeg
文件? 我使用C#
语言,asp.net
。创建缩略图
创建缩略图
回答
你必须使用GetThumbnailImage
方法在Image
类:
https://msdn.microsoft.com/en-us/library/8t23aykb%28v=vs.110%29.aspx
这里有一个粗略的例子,它接受一个图像文件,然后从它使一个缩略图,然后将其保存回磁盘。
Image image = Image.FromFile(fileName);
Image thumb = image.GetThumbnailImage(120, 120,()=>false, IntPtr.Zero);
thumb.Save(Path.ChangeExtension(fileName, "thumb"));
@ktsixit - 它在System.Drawing命名空间(在System.Drawing.dll中) – 2011-01-13 09:33:28
你是认真的吗?已经嵌入缩略图的图像GetThumbnailImage会产生可怕的结果。 [不要'犯这些错误],(请http://nathanaeljones.com/163/20-image-resizing-pitfalls/)。 http://imageresizing.net是开源的,免费,快速,并会给你高质量的结果。 – 2012-01-09 21:05:46
它一般只能用于JPG图片。如果您尝试像这样调整PNG图像的大小,则会出现此错误。 – HBlackorby 2015-06-04 21:45:27
下面的代码将在比例写的图像响应,您可以修改代码以实现你的目的:
public void WriteImage(string path, int width, int height)
{
Bitmap srcBmp = new Bitmap(path);
float ratio = srcBmp.Width/srcBmp.Height;
SizeF newSize = new SizeF(width, height * ratio);
Bitmap target = new Bitmap((int) newSize.Width,(int) newSize.Height);
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "image/jpeg";
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(srcBmp, 0, 0, newSize.Width, newSize.Height);
using (MemoryStream memoryStream = new MemoryStream())
{
target.Save(memoryStream, ImageFormat.Jpeg);
memoryStream.WriteTo(HttpContext.Response.OutputStream);
}
}
Response.End();
}
我给了我的本地文件路径在字符串路径。它返回“不支持给定的路径格式”。 – 2018-03-01 05:09:31
我给了这样的... var path = @“C:\ Users \ Gopal \ Desktop \ files.jpeg”; 位图srcBmp =新的位图(路径); – 2018-03-01 05:10:51
下面是如何创建一个更小的图像的完整的例子(缩略图)。这段代码调整图像的大小,在需要时将其旋转(如果手机垂直放置),如果要创建方形拇指则填充图像。这段代码创建了一个JPEG,但可以轻松修改其他文件类型。即使图像小于允许的最大尺寸,图像仍会被压缩,并会更改分辨率以创建相同dpi和压缩级别的图像。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
//set the resolution, 72 is usually good enough for displaying images on monitors
float imageResolution = 72;
//set the compression level. higher compression = better quality = bigger images
long compressionLevel = 80L;
public Image resizeImage(Image image, int maxWidth, int maxHeight, bool padImage)
{
int newWidth;
int newHeight;
//first we check if the image needs rotating (eg phone held vertical when taking a picture for example)
foreach (var prop in image.PropertyItems)
{
if (prop.Id == 0x0112)
{
int orientationValue = image.GetPropertyItem(prop.Id).Value[0];
RotateFlipType rotateFlipType = getRotateFlipType(orientationValue);
image.RotateFlip(rotateFlipType);
break;
}
}
//apply the padding to make a square image
if (padImage == true)
{
image = applyPaddingToImage(image, Color.Red);
}
//check if the with or height of the image exceeds the maximum specified, if so calculate the new dimensions
if (image.Width > maxWidth || image.Height > maxHeight)
{
double ratioX = (double)maxWidth/image.Width;
double ratioY = (double)maxHeight/image.Height;
double ratio = Math.Min(ratioX, ratioY);
newWidth = (int)(image.Width * ratio);
newHeight = (int)(image.Height * ratio);
}
else
{
newWidth = image.Width;
newHeight = image.Height;
}
//start the resize with a new image
Bitmap newImage = new Bitmap(newWidth, newHeight);
//set the new resolution
newImage.SetResolution(imageResolution, imageResolution);
//start the resizing
using (var graphics = Graphics.FromImage(newImage))
{
//set some encoding specs
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
//save the image to a memorystream to apply the compression level
using (MemoryStream ms = new MemoryStream())
{
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, compressionLevel);
newImage.Save(ms, getEncoderInfo("image/jpeg"), encoderParameters);
//save the image as byte array here if you want the return type to be a Byte Array instead of Image
//byte[] imageAsByteArray = ms.ToArray();
}
//return the image
return newImage;
}
//=== image padding
public Image applyPaddingToImage(Image image, Color backColor)
{
//get the maximum size of the image dimensions
int maxSize = Math.Max(image.Height, image.Width);
Size squareSize = new Size(maxSize, maxSize);
//create a new square image
Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
using (Graphics graphics = Graphics.FromImage(squareImage))
{
//fill the new square with a color
graphics.FillRectangle(new SolidBrush(backColor), 0, 0, squareSize.Width, squareSize.Height);
//put the original image on top of the new square
graphics.DrawImage(image, (squareSize.Width/2) - (image.Width/2), (squareSize.Height/2) - (image.Height/2), image.Width, image.Height);
}
//return the image
return squareImage;
}
//=== get encoder info
private ImageCodecInfo getEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType.ToLower() == mimeType.ToLower())
{
return encoders[j];
}
}
return null;
}
//=== determine image rotation
private RotateFlipType getRotateFlipType(int rotateValue)
{
RotateFlipType flipType = RotateFlipType.RotateNoneFlipNone;
switch (rotateValue)
{
case 1:
flipType = RotateFlipType.RotateNoneFlipNone;
break;
case 2:
flipType = RotateFlipType.RotateNoneFlipX;
break;
case 3:
flipType = RotateFlipType.Rotate180FlipNone;
break;
case 4:
flipType = RotateFlipType.Rotate180FlipX;
break;
case 5:
flipType = RotateFlipType.Rotate90FlipX;
break;
case 6:
flipType = RotateFlipType.Rotate90FlipNone;
break;
case 7:
flipType = RotateFlipType.Rotate270FlipX;
break;
case 8:
flipType = RotateFlipType.Rotate270FlipNone;
break;
default:
flipType = RotateFlipType.RotateNoneFlipNone;
break;
}
return flipType;
}
//== convert image to base64
public string convertImageToBase64(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
//convert the image to byte array
image.Save(ms, ImageFormat.Jpeg);
byte[] bin = ms.ToArray();
//convert byte array to base64 string
return Convert.ToBase64String(bin);
}
}
对于asp.net用户如何上传文件,调整其大小和在页面上显示的结果一个小例子。
//== the button click method
protected void Button1_Click(object sender, EventArgs e)
{
//check if there is an actual file being uploaded
if (FileUpload1.HasFile == false)
{
return;
}
using (Bitmap bitmap = new Bitmap(FileUpload1.PostedFile.InputStream))
{
try
{
//start the resize
Image image = resizeImage(bitmap, 256, 256, true);
//to visualize the result, display as base64 image
Label1.Text = "<img src=\"data:image/jpg;base64," + convertImageToBase64(image) + "\">";
//save your image to file sytem, database etc here
}
catch (Exception ex)
{
Label1.Text = "Oops! There was an error when resizing the Image.<br>Error: " + ex.Message;
}
}
}
- 1. 创建缩略图
- 2. Codeigniter - 创建缩略图Woes
- 3. 从UserControl创建缩略图
- 4. Heroku PlayFramework - 创建缩略图
- 5. 即时创建缩略图
- 6. codeigniter缩略图创建
- 7. 如何在我的模板中创建缩略图缩略图
- 8. AvalancheImagineBundle无法创建缩略图图像?
- 9. 为图像网格创建缩略图
- 10. 通过ffmpeg.exe创建缩略图图像
- 11. 未创建WordPress图片缩略图
- 12. 为灯箱图像创建缩略图
- 13. WCF服务创建视频缩略图
- 14. 为Amazon S3创建缩略图缓存
- 15. 在Android中创建PDF的缩略图
- 16. 从路径创建视频缩略图
- 17. 如何在PostgreSQL中创建缩略图
- 18. C语言创建缩略图++
- 19. 创建缩略图而不保存
- 20. 缩略图创建崩溃easy_thumbnails.processors.background
- 21. 创建网页的缩略图
- 22. ASP.Net:上传视频,创建缩略图
- 23. 用Symfony或PHP创建缩略图
- 24. WordPress的缩略图创建问题
- 25. 如何创建网站的缩略图?
- 26. 使用SWFUpload创建缩略图
- 27. 创建办公文档的缩略图
- 28. 问题在Django创建缩略图
- 29. ffmpeg-php创建视频缩略图
- 30. Android创建缩略图布局
[ImageResizer](http://imageresizing.net)是一个服务器安全库,旨在完全满足您的需求。与GetThumbnailImage不同,它可以产生高质量的结果,与代码示例不同,它不会像筛子那样泄漏内存。你现在可能并不在意,但是在核心转储时你将会在几个月时间内屈膝。 – 2013-02-12 14:27:32
http://www.codeproject.com/Articles/20971/Thumbnail-Images-in-GridView-using-C – Freelancer 2013-05-24 10:21:50