郁闷,用C#生成透明的PNG图片

YkLingyun 2005-11-05 11:47:23
代码如下:生成的图片用PhotoShop打开是透明的,但在网页上打开则是灰色背景。
//生成文件名
string sIconFileName = Server.MapPath( "test.png" );

//图片文字
string str=DateTime.Now.ToString();

Bitmap image=new Bitmap(200,30);
Graphics g=Graphics.FromImage(image);

//填充透明色
g.Clear(Color.Transparent);

//写文字
g.DrawString(str,new Font("Courier New", 10),new SolidBrush(Color.Red),20,5);

//Graphics 类还有很多绘图方法可以绘制 直线、曲线、圆等等
image.Save( sIconFileName,System.Drawing.Imaging.ImageFormat.Png );
...全文
2135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
haurau 2005-12-21
  • 打赏
  • 举报
回复
鄙视微软一下
LoveCherry 2005-11-07
  • 打赏
  • 举报
回复
PNG作为网页通用的图片格式,早已经被大家确认,但是微软不知出于何种目的,一直不肯让IE支持透明的PNG图片。 IE没有把PNG的Alpha通道打开,如果想在IE中正确显示透明的PNG图,必须手动给它添加滤镜。

1.运用这个滤镜:

〈div style="width:100%;filter :progid:DXImageTransform.Microsoft.AlphaImageLoader(src='图片地址', sizingMethod='image');"〉〈/div〉


2.htc:

<public:component><public:property name="alpha" /><public:property name="dummy" /><script type="text/javascript">var propertychange=readystatechange=function(){};var dummy=null;if((element.tagName=='IMG')&&(typeof document.all!='undefined')&&(typeof document.getElementById!='undefined')&&(navigator.platform=='Win32')){ readystatechange=function(){ if((element.readyState=='loading')||(element.readyState=='complete')){ if((element.alpha!=0)||((element.alpha==null)&&(element.src.substr(-4)=='.png'))){ element.alpha=1; if(!dummy){ dummy=document.createElement('div'); element.parentNode.insertBefore(dummy,element); dummy.runtimeStyle.position ='absolute'; dummy.runtimeStyle.zIndex =element.currentStyle.zIndex; element.runtimeStyle.zIndex =element.currentStyle.zIndex+1; dummy.runtimeStyle.filter =element.currentStyle.filter+"progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/"+element.src+"',sizingmethod=scale)"; element.runtimeStyle.filter =element.currentStyle.filter+"progid:DXImageTransform.Microsoft.Alpha(opacity=0)"; } dummy.runtimeStyle.width=element.offsetWidth+"px"; dummy.runtimeStyle.height=element.offsetHeight+"px"; dummy.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src="/element.src"; } } } propertychange=function(){ if(!dummy||(event.propertyName=='dummy'))return; dummy[event.propertyName]=element[event.propertyName]; dummy.runtimeStyle.width=element.offsetWidth+"px"; dummy.runtimeStyle.height=element.offsetHeight+"px"; dummy.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src="/element.src"; dummy.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").enabled=element.alpha; element.filters.item("DXImageTransform.Microsoft.Alpha").enabled=element.alpha!=0; }}</script><public:attach event="onpropertychange" onevent="propertychange()" /><public:attach event="onreadystatechange" onevent="readystatechange()" /></public:component>


在css中这么写:

img{behavior:url(png.htc)}
njuzgj 2005-11-07
  • 打赏
  • 举报
回复
学习+UP
LoveCherry 2005-11-07
  • 打赏
  • 举报
回复
似乎ie不支持png透明,只有火狐支持
nk912114 2005-11-07
  • 打赏
  • 举报
回复
上面代码就可以
nk912114 2005-11-07
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace dressweb
{
// class DemoThumb
// {
// [STAThread]
// static void Main(string[] args)
// {
// ThumbMaker thumbMaker = new ThumbMaker(@"c:\Image.gif");
// thumbMaker.ResizeToPng(100, 0, @"c:\ScaledImage.png");
// }
// }

public class ThumbMaker
{
Double xFactor;
Double yFactor;
System.IntPtr sourceScan0;
int sourceStride;
Bitmap scaledBitmap, bitmap;

public ThumbMaker(string fileName)
{
bitmap = new Bitmap(fileName);
}

public ThumbMaker(Stream stream)
{
bitmap = new Bitmap(stream);
}

void AdjustSizes(ref int xSize, ref int ySize)
{
if (xSize != 0 && ySize == 0)
ySize = Math.Abs((int) (xSize * bitmap.Height / bitmap.Width));
else if (xSize == 0 && ySize != 0)
xSize = Math.Abs((int) (ySize * bitmap.Width / bitmap.Height));
else if (xSize == 0 && ySize == 0)
{
xSize = bitmap.Width;
ySize = bitmap.Height;
}
}

//Internal resize for indexed colored images
void IndexedRezise(int xSize, int ySize)
{
BitmapData sourceData;
BitmapData targetData;

AdjustSizes(ref xSize, ref ySize);

scaledBitmap = new Bitmap(xSize, ySize, bitmap.PixelFormat);
scaledBitmap.Palette = bitmap.Palette;
sourceData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);
try
{
targetData = scaledBitmap.LockBits(new Rectangle(0, 0, xSize, ySize),
ImageLockMode.WriteOnly, scaledBitmap.PixelFormat);
try
{
xFactor = (Double) bitmap.Width / (Double) scaledBitmap.Width;
yFactor = (Double) bitmap.Height / (Double) scaledBitmap.Height;
sourceStride = sourceData.Stride;
sourceScan0 = sourceData.Scan0;
int targetStride = targetData.Stride;
System.IntPtr targetScan0 = targetData.Scan0;
unsafe
{
byte * p = (byte *)(void *)targetScan0;
int nOffset = targetStride - scaledBitmap.Width;
int nWidth = scaledBitmap.Width;
for(int y=0;y < scaledBitmap.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
p[0] = GetSourceByteAt(x, y);
++p;
}
p += nOffset;
}
}
}
finally
{
scaledBitmap.UnlockBits(targetData);
}
}
finally
{
bitmap.UnlockBits(sourceData);
}
}

//This gets the color index on the source image for coords x, y on the resized target image
byte GetSourceByteAt(int x, int y)
{
unsafe
{
return ((byte*) ((int)sourceScan0 + (int) (Math.Floor(y * yFactor) * sourceStride) +
(int) Math.Floor(x * xFactor)))[0];
}
}

//Internal resize for RGB colored images
void RGBRezise(int xSize, int ySize)
{
AdjustSizes(ref xSize, ref ySize);
scaledBitmap = new Bitmap(xSize, ySize, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(scaledBitmap);
Rectangle destRect = new Rectangle(0, 0, xSize, ySize);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawImage(bitmap, destRect);
}

void Save(string fileName, ImageFormat format)
{
scaledBitmap.Save(fileName, format);
}

void Save(string fileName, long jQuality, ImageFormat format)
{
ImageCodecInfo jpegCodecInfo = GetEncoderInfo("image/jpeg");
Encoder qualityEncoder = Encoder.Quality;
EncoderParameters encoderParams = new EncoderParameters(1);
EncoderParameter qualityEncoderParam = new EncoderParameter(qualityEncoder, jQuality);
encoderParams.Param[0] = qualityEncoderParam;
scaledBitmap.Save(fileName, jpegCodecInfo, encoderParams);
}

void Save(Stream stream, ImageFormat format)
{
scaledBitmap.Save(stream, format);
}

void Save(Stream stream, long jQuality, ImageFormat format)
{
ImageCodecInfo jpegCodecInfo = GetEncoderInfo("image/jpeg");
Encoder qualityEncoder = Encoder.Quality;
EncoderParameters encoderParams = new EncoderParameters(1);
EncoderParameter qualityEncoderParam = new EncoderParameter(qualityEncoder, jQuality);
encoderParams.Param[0] = qualityEncoderParam;
scaledBitmap.Save(stream, jpegCodecInfo, encoderParams);
}

ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType.ToUpper() == mimeType.ToUpper())
return encoders[j];
}
return null;
}

public void ResizeToJpeg(int xSize, int ySize, string fileName)
{
this.RGBRezise(xSize, ySize);
this.Save(fileName, ImageFormat.Jpeg);
}

public void ResizeToJpeg(int xSize, int ySize, Stream stream)
{
this.RGBRezise(xSize, ySize);
this.Save(stream, ImageFormat.Jpeg);
}

public void ResizeToJpeg(int xSize, int ySize, long jQuality, string fileName)
{
this.RGBRezise(xSize, ySize);
this.Save(fileName, jQuality, ImageFormat.Jpeg);
}

public void ResizeToJpeg(int xSize, int ySize, long jQuality, Stream stream)
{
this.RGBRezise(xSize, ySize);
this.Save(stream, jQuality, ImageFormat.Jpeg);
}


public void ResizeToGif(int xSize, int ySize, string fileName)
{
this.IndexedRezise(xSize, ySize);
this.Save(fileName, ImageFormat.Gif);
}

public void ResizeToGif(int xSize, int ySize, Stream stream)
{
this.IndexedRezise(xSize, ySize);
this.Save(stream, ImageFormat.Gif);
}

public void ResizeToPng(int xSize, int ySize, string fileName)
{
this.IndexedRezise(xSize, ySize);
this.Save(fileName, ImageFormat.Png);
}

public void ResizeToPng(int xSize, int ySize, Stream stream)
{
this.IndexedRezise(xSize, ySize);
this.Save(stream, ImageFormat.Png);
}

}
}
YkLingyun 2005-11-07
  • 打赏
  • 举报
回复
谢谢,都是微软惹的祸
YkLingyun 2005-11-06
  • 打赏
  • 举报
回复
UP
ChengKing 2005-11-05
  • 打赏
  • 举报
回复
另一种生成方式:
http://blog.csdn.net/chengking/archive/2005/10/07/496615.aspx
YkLingyun 2005-11-05
  • 打赏
  • 举报
回复
一定是要生成图片,不是用于认证

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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