Write Alpha Jpg : JPG « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » 2D Graphics » JPGScreenshots 
Write Alpha Jpg
       
/* Copyright (C) 2008 Robin Debreuil -- Released under the BSD License */

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

namespace DDW.Swf
{
  public class Utils
  {
    public const uint TwipsPerPixel = 20;


    public static void WriteAlphaJpg(byte[] alphaData, Bitmap jpg, string pngDest)
    {
      int w = jpg.Width;
      int h = jpg.Height;
      Bitmap png = new Bitmap(w, h, PixelFormat.Format32bppArgb);
      

       BitmapData jpgData = jpg.LockBits(
         new Rectangle(00, w, h),
         ImageLockMode.ReadOnly, 
         PixelFormat.Format24bppRgb);

       BitmapData pngData = png.LockBits(
         new Rectangle(00, w, h),
         ImageLockMode.WriteOnly, 
         PixelFormat.Format32bppArgb);

      unsafe
      {
        byte* jpgStart = (byte*)jpgData.Scan0.ToPointer();
        byte* pngStart = (byte*)pngData.Scan0.ToPointer();
        int jpgStride = jpgData.Stride;
        int pngStride = pngData.Stride;

        for (int y = 0; y < h; y++)
        {
          byte* jpgRow = (byte*)(jpgStart + y * jpgStride);
          byte* pngRow = (byte*)(pngStart + y * pngStride);

          for (int x = 0; x < w; x++// 3 bpp
          {
            int pngX = x * 4;
            int jpgX = x * 3;
            pngRow[pngX= jpgRow[jpgX];
            pngRow[pngX + 1= jpgRow[jpgX + 1];
            pngRow[pngX + 2= jpgRow[jpgX + 2];
            pngRow[pngX + 3= alphaData[y*w + x];
          }
        }
      }       
      jpg.UnlockBits(jpgData);
      png.UnlockBits(pngData);

      DDW.Vex.Utils.EnsurePath(pngDest);
      png.Save(pngDest);
      png.Dispose();
    }

    public static Vex.Size GetJpgSize(string path)
    {
      Bitmap bmp = new Bitmap(path, false);
      Vex.Size sz = new DDW.Vex.Size(bmp.Width, bmp.Height);
      bmp.Dispose();
      return sz;
    }

    public static Matrix ScaleBitmapMatrix(Matrix m)
    {
      System.Drawing.Drawing2D.Matrix mx = 
        new System.Drawing.Drawing2D.Matrix(m.ScaleX, m.Rotate0, m.Rotate1, m.ScaleY, m.TranslateX, m.TranslateY);
      mx.Scale(20F20F);
      float[] els = mx.Elements;
      return new Matrix(els[0], els[1], els[2], els[3], els[4], els[5]);
    }
  }
}

   
    
    
    
    
    
    
  
Related examples in the same category
1.Load JPG file and paint the imageLoad JPG file and paint the image
2.Convert bmp image format to jpg, gif, png format images
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.