日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中保存Gif文件设置透明无效果

發布時間:2025/4/5 C# 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中保存Gif文件设置透明无效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

在C#中使用Graphics.Clear(Color.Transparent)生成gif格式透明文件時,設置透明不起作用。使用Bitmap.MakeTransparent 方法同樣也不能使gif文件透明。這兩個問題也是微軟的設計使然,和gif文件格式有關系。

如果要在c#中編程實現透明色,必須另寫代碼,下面是一位老外的代碼


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->///?<summary>
///?Make?the?image?transparent.?
///?The?input?is?the?color?which?you?want?to?make?transparent.
///?</summary>
///?<param?name="color">The?color?to?make?transparent.</param>
///?<param?name="bitmap">The?bitmap?to?make?transparent.</param>
///?<returns>New?memory?stream?containing?transparent?background?gif.</returns>
public?Bitmap?MakeTransparentGif(Bitmap?bitmap,?Color?color)
{
????
byte?R?=?color.R;
????
byte?G?=?color.G;
????
byte?B?=?color.B;

????MemoryStream?fin?
=?new?MemoryStream();
????bitmap.Save(fin,?System.Drawing.Imaging.ImageFormat.Gif);

????MemoryStream?fout?
=?new?MemoryStream((int)fin.Length);
????
int?count?=?0;
????
byte[]?buf?=?new?byte[256];
????
byte?transparentIdx?=?0;
????fin.Seek(
0,?SeekOrigin.Begin);
????
//header
????count?=?fin.Read(buf,?0,?13);
????
if?((buf[0]?!=?71)?||?(buf[1]?!=?73)?||?(buf[2]?!=?70))?return?null;?//GIF

????fout.Write(buf,?
0,?13);

????
int?i?=?0;
????
if?((buf[10]?&?0x80)?>?0)
????{
????????i?
=?1?<<?((buf[10]?&?7)?+?1)?==?256???256?:?0;
????}

????
for?(;?i?!=?0;?i--)
????{
????????fin.Read(buf,?
0,?3);
????????
if?((buf[0]?==?R)?&&?(buf[1]?==?G)?&&?(buf[2]?==?B))
????????{
????????????transparentIdx?
=?(byte)(256?-?i);
????????}
????????fout.Write(buf,?
0,?3);
????}

????
bool?gcePresent?=?false;
????
while?(true)
????{
????????fin.Read(buf,?
0,?1);
????????fout.Write(buf,?
0,?1);
????????
if?(buf[0]?!=?0x21)?break;
????????fin.Read(buf,?
0,?1);
????????fout.Write(buf,?
0,?1);
????????gcePresent?
=?(buf[0]?==?0xf9);
????????
while?(true)
????????{
????????????fin.Read(buf,?
0,?1);
????????????fout.Write(buf,?
0,?1);
????????????
if?(buf[0]?==?0)?break;
????????????count?
=?buf[0];
????????????
if?(fin.Read(buf,?0,?count)?!=?count)?return?null;
????????????
if?(gcePresent)
????????????{
????????????????
if?(count?==?4)
????????????????{
????????????????????buf[
0]?|=?0x01;
????????????????????buf[
3]?=?transparentIdx;
????????????????}
????????????}
????????????fout.Write(buf,?
0,?count);
????????}
????}
????
while?(count?>?0)
????{
????????count?
=?fin.Read(buf,?0,?1);
????????fout.Write(buf,?
0,?1);
????}
????fin.Close();
????fout.Flush();

????
return?new?Bitmap(fout);
}

在生成圖片之后調用此方法,即可使gif顯示透明色。

-------------------------------------------------------------

***系統(DianCMS) 1.0 盛大發布

http://www.diancms.com

轉載于:https://www.cnblogs.com/grassland619/archive/2009/06/20/1507284.html

總結

以上是生活随笔為你收集整理的C#中保存Gif文件设置透明无效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。