Share This!

Friday, December 30, 2016

Making Transparent PNGs in C#

This bit of code shows how to make a PNG with transparency totally from scratch in code.  This code will save the image back as a file, but it could as easily be streamed back to a web request.  This is a complete console app, if you want to compile it, you'll need to add a reference to System.Drawing.



using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace TransparentPNGTests
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var bmp = new System.Drawing.Bitmap(100, 100, PixelFormat.Format32bppArgb))
            using (Graphics g = Graphics.FromImage(bmp))
            using (Font f = new Font("Univers", 14f, FontStyle.Regular, GraphicsUnit.Pixel))
            {
                //set up bitmap
                g.Clear(Color.Transparent);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                //draw text                
                Brush b = Brushes.Black;
                Rectangle textrect = new Rectangle(5, 5, 90, 90);  
                
                //using the rectangle instead of a point is a quick way to do word wrapping on the graphic
                g.DrawString("This is a bunch of text, for testing.", f, b, textrect);

                //save from the bitmap
                g.Flush(FlushIntention.Flush);   //this seems to be wise, but unnecessary.
                bmp.Save("bmpsave.png", ImageFormat.Png);                

                //save from the image - loses transparency
                //Image image = Image.FromHbitmap(bmp.GetHbitmap());
                //image.Save("image.png");
            }
        }
    }

}


Note that the last 2 lines:

Image image = Image.FromHbitmap(bmp.GetHbitmap());
image.Save("image.png");
Are the wrong way.   This is the way you'll find if you Google for "How do I save a Graphic object to a file or stream".  Somehow, the GetHbitmap function manages to mangle the formatting out of the image.
Here are the resulting images from this code.
Bitmap.Save()

Image Save()

Monday, December 19, 2016

The item $/... does not exist at the specified version, or you do not have permission to access it.

Created some new views, and most of them worked OK, except one.

The item $/... does not exist at the specified version, or you do not have permission to access it.

Somehow the Team Foundation Server (TFS) thought the file already existed, even though I was trying to add it to source control for the first time.

How to solve.

  1. Copy and paste the text of the file to notepad
  2. Delete the file in Solution Explorer (this causes a check-out of the .csproj file)
  3. Check-in (this adds the "delete" to TFS).
  4. Now, in Solution Explorer, add the view (empty, without model)
  5. Note that there is now a [+] icon by the view in Solution Explorer
  6. Paste in the text from Notepad
  7. Save
  8. Right click the project (not just the added file) and check in. There should be 2 files in the changeset, the view and the .csproj.



Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Monday, December 5, 2016

Kernel Auto Boost Lock Acquisition With Raised IRQL

For me, this error was the direct result of my XiMeta NDAS driver software. Anytime my internet went out, Windows 10 would immediately bluescreen with the message:

Kernel Auto Boost Lock Acquisition With Raised IRQL

I uninstalled the NDAS driver, which was only required if the shared drive is plugged into the DSL router, not if the drive is plugged into the USB port.


Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Contact Us

Name

Email *

Message *