Graphics绘图保存的时候,怎么设置Gif图片中的透明色呀

athlonliu 2003-08-19 04:40:10
同题
...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
athlonliu 2003-08-21
  • 打赏
  • 举报
回复
up
athlonliu 2003-08-21
  • 打赏
  • 举报
回复
不能用呀,大哥

我是
dim btMap as new Bitmap(400,300)
dim g as graphics
g=Graphics.FromImage(btMap)来的
panyee 2003-08-20
  • 打赏
  • 举报
回复
g.Clear(Color.Transparent);
hanxing1026 2003-08-20
  • 打赏
  • 举报
回复
顶一把吧。
athlonliu 2003-08-20
  • 打赏
  • 举报
回复
176 Public Shared Sub CopyArrayTo(<[In](), MarshalAs(UnmanagedType.I4)> ByVal hpvDest As Int32, <[In](), Out()> ByVal hpvSource() As Byte, ByVal cbCopy As Integer)

报语法错误
孟子E章 2003-08-19
  • 打赏
  • 举报
回复
http://www.aspalliance.com/chrisg/default.asp?article=142
孟子E章 2003-08-19
  • 打赏
  • 举报
回复
1 Imports System.Runtime.InteropServices
2 Imports System
3 Imports System.IO
4 Imports System.Drawing
5 Imports System.Drawing.Imaging
6 Imports System.Drawing.Drawing2D
7
8 Public Class transparentGif : Inherits System.Web.UI.Page
9
10
11
12 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
13
14 dim pic As New System.Drawing.Bitmap(200, 200, PixelFormat.Format24bppRgb)
15
16 ' ''draw image
17 dim blackpen As New Pen(Color.Black, 3)
18 dim redpen As New Pen(Color.Red, 4)
19 dim silverpen As New Pen(ColorTranslator.FromHtml("#CCCCCC"), 10)
20 dim fBrush As SolidBrush = New SolidBrush(ColorTranslator.FromHtml("#0000FF"))
21 dim g As Graphics = Graphics.FromImage(pic)
22
23 g.Clear(Color.White) ' blank the image
24 g.DrawLine(silverpen, 7, 80, 110, 80)
25 g.SmoothingMode = SmoothingMode.AntiAlias ' antialias objects
26 g.DrawString("TEST", New Font("verdana", 24, FontStyle.Bold), fBrush, New PointF(10, 50))
27 g.DrawEllipse(blackpen, 5, 5, 110, 110)
28 g.DrawEllipse(redpen, 1, 1, 118, 118)
29
30
31
32 ' ''save New image
33 pic = recolorGif(pic)
34
35 ' Set the content type
36 Response.ContentType = "image/gif"
37
38 ' send the image to the viewer
39 pic.Save(Response.OutputStream, ImageFormat.Gif)
40
41 ' tidy up
42 pic.Dispose()
43
44 End Sub
45
46
47
48 Private Function GetColorPalette() As ColorPalette
49 ' Make a New Bitmap object to get its Palette.
50 dim bitmap As Bitmap = New Bitmap(1, 1, PixelFormat.Format8bppIndexed)
51 dim palette As ColorPalette = bitmap.Palette ' Grab the palette
52 bitmap.Dispose()
53 Return palette ' Send the palette back
54 End Function
55
56
57 Private Function recolorGif(ByVal image As Image) As Bitmap
58 dim nColors As Integer = 16
59
60 ' Make a New 8-BPP indexed bitmap that is the same size as the source image.
61 dim Width As Integer = image.Width
62 dim Height As Integer = image.Height
63
64 dim bitmap As Bitmap = New Bitmap(Width, Height, PixelFormat.Format8bppIndexed)
65
66 ' Create a color palette big enough to hold the colors you want.
67 dim pal As ColorPalette = GetColorPalette()
68
69 ' Initialize a New color table with entries
70 dim i As Integer
71
72 ' Set palette the lazy way!
73 ' replace with a proper color algorithm
74 for i = 0 to nColors - 1
75 pal.Entries(i) = Color.FromArgb(255, 100, 100, 100)
76 next
77
78 pal.Entries(0) = Color.FromArgb(255, 0, 0, 0)
79 pal.Entries(1) = Color.FromArgb(255, 255, 0, 0)
80 pal.Entries(2) = Color.FromArgb(255, 0, 255, 0)
81 pal.Entries(3) = Color.FromArgb(255, 0, 0, 255)
82 pal.Entries(4) = Color.FromArgb(255, 204, 204, 204)
83 pal.Entries(nColors - 1) = Color.FromArgb(0, 255, 255, 255)
84
85 ' web safe palette use values =
86 ' 00 51 102 153 204 255
87
88 ' Set the palette into the New Bitmap object.
89 bitmap.Palette = pal
90
91 dim BmpCopy As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
92
93 dim g As Graphics
94 g = Graphics.FromImage(BmpCopy)
95
96 g.PageUnit = GraphicsUnit.Pixel
97
98 ' Transfer the Image to the Bitmap.
99 g.DrawImage(image, 0, 0, Width, Height)
100
101 ' Force g to release its resources, namely BmpCopy.
102 g.Dispose()
103
104 ' Lock a rectangular portion of the bitmap for writing.
105 dim bitmapData As BitmapData
106 dim rect As Rectangle = New Rectangle(0, 0, Width, Height)
107
108 bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed)
109
110 ' Copy the pixels from the source image
111 dim pixels As IntPtr = bitmapData.Scan0
112 dim bits As Byte() ' the buffer
113 dim pBits As Int32
114
115 if (bitmapData.Stride > 0) then
116 pBits = pixels.ToInt32()
117 Else
118 pBits = pixels.ToInt32() + bitmapData.Stride * (Height - 1)
119 end if
120
121 dim stride As Integer = Math.Abs(bitmapData.Stride)
122 Redim bits(Height * stride) ' Allocate the working buffer.
123
124 dim row As Integer
125 dim col As Integer
126
127 for row = 0 to Height - 1
128 for col = 0 to Width - 1
129
130 dim pixel As Color
131 dim i8BppPixel As Integer = row * stride + col
132
133 pixel = BmpCopy.GetPixel(col, row)
134
135 dim colorIndex As Double
136 if pixel.R = 0 And pixel.G = 0 And pixel.B = 0 then
137 colorIndex = 0
138 Elseif pixel.R > 100 And pixel.G = 0 And pixel.B = 0 then
139 colorIndex = 1
140 Elseif pixel.G > 100 And pixel.R = 0 And pixel.B = 0 then
141 colorIndex = 2
142 Elseif pixel.B > 100 And pixel.R = 0 And pixel.G = 0 then
143 colorIndex = 3
144 Elseif pixel.B = 204 And pixel.R = 204 And pixel.G = 204 then
145 colorIndex = 4
146 Else
147 colorIndex = (nColors - 1)
148 end if
149
150 bits(i8BppPixel) = CByte(colorIndex)
151
152 next col
153 next row
154
155 ' Put the image bits definition into the bitmap.
156 dim win32 As win32api = New win32api()
157 win32.CopyArrayTo(pBits, bits, Height * stride)
158 bitmap.UnlockBits(bitmapData)
159
160 Return bitmap
161
162 BmpCopy.Dispose()
163 bitmap.Dispose()
164
165 End Function
166
167 End Class
168
169 Public Class win32api
170
171
172 <DllImport("KERNEL32.DLL", EntryPoint:="RtlMoveMemory", _
173 SetLastError:=True, CharSet:=CharSet.Auto, _
174 ExactSpelling:=True, _
175 CallingConvention:=CallingConvention.StdCall)> _
176 Public Shared Sub CopyArrayTo(<[In](), MarshalAs(UnmanagedType.I4)> ByVal hpvDest As Int32, <[In](), Out()> ByVal hpvSource() As Byte, ByVal cbCopy As Integer)
177 ' Leave Function empty
178 End Sub
179
180
181
182 End Class

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧