Can you make an alpha transparent PNG with C#?

后端 未结 9 1736
陌清茗
陌清茗 2020-12-02 14:31

I have a multi-browser page that shows vertical text.

As an ugly hack to get text to render vertically in all browsers I\'ve created a custom page handler that retur

相关标签:
9条回答
  • 2020-12-02 14:51

    You can use the LockBits method on your Bitmap to return a BitmapData object, and set the Alpha for each pixel yourself (you could also use GetPixel and SetPixel, but these methods are insanely slow). See this answer.

    I know this works for sure, because when I first started using the technique I was setting the alpha value to 0, so none of the colors I was setting were visible.

    Edit: here is a sample of a fisheye lens effect, done entirely in .NET (using LockBits):

    alt text http://www.freeimagehosting.net/uploads/21ca8300cc.jpg

    0 讨论(0)
  • 2020-12-02 14:57

    It turns out that Microsoft have a new dynamic image library for the web as part of the Asp.Net content on CodePlex:

    http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449

    Unfortunately it's a little bit much for what I want. Using unsafe code I can roll my own alpha transparency, but that's far too slow given this code's use as part of a high-usage web application.

    Is this just a bug in MS's GDI+ .Net implementation?

    0 讨论(0)
  • 2020-12-02 14:58

    To fix the text "blockiness", can't you just do...

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    

    after this line...

    Graphics g = Graphics.FromImage( (Image) image );
    
    0 讨论(0)
  • 2020-12-02 14:58

    Sorry, I didn't read your question carefully enough.

    I know that I have successfully drawn rotated text on a colored background without seeing either of the aliasing effects in your examples. When I went back to look at my code, I realized that I was drawing the rotated text using a logical font, which is nicely wrapped in the Microsoft.WindowsCE.Forms namespace. This technique sets the drawing angle as a property of the logical font (so you don't call TranslateTransform or RotateTransform).

    Outside of the Windows CE world, you have to PInvoke to create a logical font (I've never done this and I couldn't find a good example quickly). I don't know how well this would perform, but I know for sure that it will draw rotated text without the weird aliasing effects you're seeing. I think for sure this is a bug in GDI+ (or just something they didn't think anyone would ever really use).

    An alternative approach that might even perform better than the logical font is to draw the text normally on a colored rectangle background (just large enough to hold the text), then rotate-and-copy the rectangle onto your main image. Graphics.DrawImage doesn't appear to be able to do rotation, but this effect is easy to implement using LockBits.

    0 讨论(0)
  • 2020-12-02 14:58

    You should at least specify a 32bits pixel format (ex: PixelFormat.Format32bppArgb) when creating your bitmap, otherwise GDI+ cannot manage transparency.

    To tune the resulting file when saving, you can use Save with ImageCodecInfo and EncoderParameters.

    0 讨论(0)
  • 2020-12-02 15:06

    IIRC you need to specify the PixelFormat in the bitmap constructor.

    0 讨论(0)
提交回复
热议问题