title: Resize transparent images using C#
body: <p>Does anyone have the secret formula to resizing transparent images (mainly GIFs) <em>without</em> ANY quality loss - what so ever?</p>
  
  <p>I've tried a bunch of stuff, the closest I get is not good enough.</p>
  
  <p>Take a look at my main image:</p>
  
  <p><a href="http://www.thewallcompany.dk/test/main.gif" rel="noreferrer">http://www.thewallcompany.dk/test/main.gif</a></p>
  
  <p>And then the scaled image:</p>
  
  <p><a href="http://www.thewallcompany.dk/test/ScaledImage.gif" rel="noreferrer">http://www.thewallcompany.dk/test/ScaledImage.gif</a></p>
  
  <pre><code>//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 &lt; scaledBitmap.Height; ++y)
          {
            for (int x = 0; x &lt; nWidth; ++x)
            {
              p[0] = GetSourceByteAt(x, y);
              ++p;
            }
            p += nOffset;
          }
        }
      }
      finally
      {
        scaledBitmap.UnlockBits(targetData);
      }
    }
    finally
    {
      bitmap.UnlockBits(sourceData);
    }
  }
  </code></pre>
  
  <p>I'm using the above code, to do the indexed resizing.</p>
  
  <p>Does anyone have improvement ideas?</p>