16,554
社区成员
发帖
与我相关
我的任务
分享
Private Sub GenerateImage()
Try
Dim gifImage As Image = Image.FromFile(Server.MapPath("~/11.jpg"))
Dim bm As New Bitmap(gifImage.Width, gifImage.Height, PixelFormat.Format8bppIndexed)
' Get the paletter from the bitmap
Dim pal As ColorPalette = bm.Palette
' Set Alpha to 0
For i As Integer = 0 To pal.Entries.Length - 1
Dim col As Color = pal.Entries(i)
pal.Entries(i) = Color.FromArgb(0, col.R, col.G, col.B)
Next
' assign palette
bm.Palette = pal
' to copy the bitmap data we need to lock the source & destination bits
Dim src As BitmapData = CType(gifImage, Bitmap).LockBits(New Rectangle(0, 0, gifImage.Width, gifImage.Height), ImageLockMode.ReadOnly, gifImage.PixelFormat)
Dim dst As BitmapData = bm.LockBits(New Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.WriteOnly, bm.PixelFormat)
' finished, unlock the bits
CType(gifImage, Bitmap).UnlockBits(src)
bm.UnlockBits(dst)
' Set Response Type to "image/gif" and write the gif directly into the Output-Stream
Response.ContentType = "image/jpeg"
bm.Save(Response.OutputStream, ImageFormat.Jpeg)
' cleaning up
bm.Dispose()
gifImage.Dispose()
' Send output stream
Response.Flush()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub