bmp.aspx
作者:淘特網
來源:淘特網
註:轉載請註明出處
首先準備一張點陣圖影像source.bmp,將它保存在bmp.aspx同一目錄中
<%@ Page language="c#" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
// 設定mime 類型為image/jpeg,即將向瀏覽器輸出JPGE格式的圖片
Response.Clear();
Response.ContentType="image/jpeg";
Bitmap OutputBitmap = new Bitmap(Server.MapPath("source.bmp"));//新建BitMap對象
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
長[] quality = new 長[1];
int comp = 0;
if (Request.QueryString["comp"] != "") { comp = Convert.ToInt16(Request.QueryString["comp"]); }
quality[0] = comp; //0 to 100 最高品質為100
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//取得包含內建影像編碼解碼器的資訊的ImageCodecInfo 物件。
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//設定JPEG編碼
break;
}
}
if (jpegICI != null)
{
OutputBitmap.Save(Response.OutputStream, jpegICI, encoderParams);//將點陣圖物件以流格式並用JPEG編解碼參數儲存到輸出流。
}
// clean up
OutputBitmap.Dispose();
}
</script>
在瀏覽器位址輸入: http://localhost/bmp.aspx?comp=0
將會看到影像,調整comp的值,將會看到不同的效果.