导航
  • 主页
  • WinForm
  • WPF
  • 问答

如何保存为BMP格式的图像文件?

希偌 深圳市车厘子网络科技有些公司 CTO/CIO/技术副总裁/总工程师  2003-08-05 12:14:42
System.Drawing.Bitmap bmp=new System.Drawing.Bitmap(cakex,cakey);
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bmp);
bmp.Save("C:\\sample.jpg",ImageFormat.Jpeg);//保存为jpg格式就可以
bmp.Save("C:\\sample.bmp",ImageFormat.Bmp);//保存为bmp就不行,何解?

感谢帮助
...全文
79 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2003-08-05
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=45E7E33C-F149-450E-B5D5-832958C20538
回复
孟子E章 2003-08-05
代码如下:Thumbnail.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Thumbnail.aspx.vb"
Inherits="aspxWeb.Thumbnail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>在ASP.NET里轻松实现缩略图</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<asp:Label id="Label1" runat="server"></asp:Label>
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<INPUT type="file" name="file" width="600"><br><br>
<asp:Button id="Button1" runat="server"></asp:Button>
</form>
</body>
</HTML>

后代码:Thumbnail.aspx.vb

Imports System
Imports System.Web
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Imaging

Public Class Thumbnail
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>"
Button1.Text = "上载并显示缩略图"
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyFileColl As HttpFileCollection = HttpContext.Current.Request.Files
Dim MyPostedFile As HttpPostedFile = MyFileColl.Item(0)
If LCase(MyPostedFile.ContentType.ToString()).IndexOf("image") < 0 Then
Response.Write("无效的图形格式。")
Exit Sub
End If
GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(),_
False, MyPostedFile.InputStream)
End Sub

Private Function GetImageType(ByVal strContentType) As System.Drawing.Imaging.ImageFormat
Select Case (strContentType.ToString().ToLower())
Case "image/pjpeg"
GetImageType = System.Drawing.Imaging.ImageFormat.Jpeg
Case "image/gif"
GetImageType = System.Drawing.Imaging.ImageFormat.Gif
Case "image/bmp"
GetImageType = System.Drawing.Imaging.ImageFormat.Bmp
Case "image/tiff"
GetImageType = System.Drawing.Imaging.ImageFormat.Tiff
Case "image/x-icon"
GetImageType = System.Drawing.Imaging.ImageFormat.Icon
Case "image/x-png"
GetImageType = System.Drawing.Imaging.ImageFormat.Png
Case "image/x-emf"
GetImageType = System.Drawing.Imaging.ImageFormat.Emf
Case "image/x-exif"
GetImageType = System.Drawing.Imaging.ImageFormat.Exif
Case "image/x-wmf"
GetImageType = System.Drawing.Imaging.ImageFormat.Wmf
Case Else
GetImageType = System.Drawing.Imaging.ImageFormat.MemoryBmp
End Select
End Function

Private Sub GetThumbNail(ByVal strFileName, ByVal iWidth, ByVal iheight, ByVal strContentType, _
ByVal blnGetFromFile, ByVal ImgStream)
Dim oImg As Image
If blnGetFromFile Then
oImg = oImg.FromFile(strFileName)
Else
oImg = oImg.FromStream(ImgStream)
End If
oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr()).Zero)
Dim strGuid As String = (New Guid()).NewGuid().ToString().ToUpper()
Dim strFileExt As String = strFileName.Substring(strFileName.LastIndexOf("."))
'保存到本地
'oImg.Save(Server.MapPath("images") + "\" + strGuid + strFileExt, GetImageType(strContentType))
'直接输出url文件
'Response.Redirect("images/" + strGuid + strFileExt)
'以下显示在屏幕上
Response.ContentType = strContentType
Dim MemStream As New MemoryStream()
' 注意:这里如果直接用 oImg.Save(Response.OutputStream, GetImageType(strContentType))
' 对不同的格式可能会出错,比如Png格式。
oImg.Save(MemStream, GetImageType(strContentType))
MemStream.WriteTo(Response.OutputStream)
End Sub

End Class

回复
xiruo 2003-08-05
如何在浏览器中浏览生成的bmp图像,而不是打开画图来浏览?
回复
希偌 2003-08-05
改为
MemoryStream MemStream=new MemoryStream();
bmp.Save(MemStream,ImageFormat.Bmp);
MemStream.WriteTo(stream);
就可以了
回复
希偌 2003-08-05
改为
MemoryStream MemStream=new MemoryStream();
bmp.Save(MemStream,ImageFormat.Bmp);
MemStream.WriteTo(stream);
就可以了
回复
希偌 2003-08-05
不愧为俺的偶像,果然如此,感谢
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.