我用下面的代码可以把jpg的图片缩略,可是gif就会不可以
请大家帮个忙
private void ShowThumbnail(string oldfile, string newfile, int newh, int neww)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(oldfile));
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero); myThumbnail.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose();
myThumbnail.Dispose();
}
private bool ThumbnailCallback()
{
return false;
}
请大家帮个忙
private void ShowThumbnail(string oldfile, string newfile, int newh, int neww)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(oldfile));
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero); myThumbnail.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose();
myThumbnail.Dispose();
}
private bool ThumbnailCallback()
{
return false;
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging; namespace Test
{
/// <summary>
/// Summary description for Test2.
/// </summary>
public class Test2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile fileCSV;
protected System.Web.UI.HtmlControls.HtmlInputButton btnImport;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Label1.Text = "<h3>Display the picture</h3>";
Button1.Text = "upload the picture"; } #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
HttpPostedFile MyPostedFile = MyFileColl[0]; if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
Response.Write("incorrect format.");
return;
}
GetThumbNail(MyPostedFile.FileName, 100, 100,
MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream);
}
private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
{
if ((strContentType.ToString().ToLower()) == "image/pjpeg")
{
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
else if ((strContentType.ToString().ToLower()) == "image/gif")
{
return System.Drawing.Imaging.ImageFormat.Gif;
}
else if ((strContentType.ToString().ToLower()) == "image/bmp")
{
return System.Drawing.Imaging.ImageFormat.Bmp;
}
else if ((strContentType.ToString().ToLower()) == "image/tiff")
{
return System.Drawing.Imaging.ImageFormat.Tiff;
}
else if ((strContentType.ToString().ToLower()) == "image/x-icon")
{
return System.Drawing.Imaging.ImageFormat.Icon;
}
else if ((strContentType.ToString().ToLower()) == "image/x-png")
{
return System.Drawing.Imaging.ImageFormat.Png;
}
else if ((strContentType.ToString().ToLower()) == "image/x-emf")
{
return System.Drawing.Imaging.ImageFormat.Emf;
}
else if ((strContentType.ToString().ToLower()) == "image/x-exif")
{
return System.Drawing.Imaging.ImageFormat.Exif;
}
else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
{
return System.Drawing.Imaging.ImageFormat.Wmf;
}
else
{
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
}
} private void GetThumbNail(string strFileName, int iWidth, int iheight,
string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream)
{
System.Drawing.Image oImg; if (blnGetFromFile)
{
oImg = System.Drawing.Image.FromFile(strFileName);
}
else
{
oImg = System.Drawing.Image.FromStream(ImgStream);
}
oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
string strGuid = System.Guid.NewGuid().ToString().ToUpper();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
Response.ContentType = strContentType;
MemoryStream MemStream = new MemoryStream();
oImg.Save(MemStream, GetImageType(strContentType));
MemStream.WriteTo(Response.OutputStream);
} }
}aspx文件
<%@ Page language="c#" Codebehind="Test2.aspx.cs" AutoEventWireup="false" Inherits="Test.Test2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" runat="server">Label</asp:Label><br>
<input id="fileCSV" type="file" runat="server" NAME="fileCSV"><br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>