Archive for the '.Net' Category

 

Pointers in C# (CSharp) .. example with bits..

Feb 12, 2009 in .Net, Microsoft

// 32 bit source Bitmap
System.Drawing.Bitmap b

int bWidth, bHeight;
int idx = 0;

bWidth = b.Width;
bHeight = b.Height;

byte[,] Disp = new byte[bWidth, bHeight];

// create a new 16 bit bitmap
Bitmap b565 = new Bitmap(b.Size.Width,b.Size.Height,System.Drawing.Imaging.PixelFormat.Format16bppRgb565);

Graphics g = Graphics.FromImage(b565);
// draw the 32 bit source bitmap to the 16 bit bitmap
g.DrawImage(b, new Point(0, 0));
g.Dispose();

BitmapData bmd = b565.LockBits(new Rectangle(0, 0, 480, 272), System.Drawing.Imaging.ImageLockMode.ReadOnly, b565.PixelFormat);

byte[] Result = new byte[bWidth * bHeight * 2];

unsafe
{
   // get a pointer to the beginning of image data in the 16 bit bitmap
   byte* bitdata = (byte*)bmd.Scan0;

   for (int y = 0; y < bmd.Height*bmd.Width*2; y++)
   {
       // just copy the image data from the Bitmap to the byte array
       Result[y] = bitdata[y];
   }
}

b565.UnlockBits(bmd);
USBD480_DrawFullScreen(ref di, Result);

Converting jpg to png in C# (CSharp) ..

Jan 12, 2009 in .Net, Uncategorized

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

// apt-get install libgdiplus

class CGTest1 {

    public static void Main() {

        /* Image bmpImageToConvert =
           System.Drawing.Image.FromFile(
           Server.MapPath(strUploadImagePath));
         */

        Image bmpImageToConvert = Image.FromFile("images/sample3.jpg");
        Image bmpNewImage = new Bitmap(bmpImageToConvert.Width,
                                       bmpImageToConvert.Height);
        Graphics gfxNewImage = Graphics.FromImage(bmpNewImage);
        gfxNewImage.DrawImage(bmpImageToConvert,
                              new Rectangle(0, 0, bmpNewImage.Width,
                                            bmpNewImage.Height),
                              0, 0,
                              bmpImageToConvert.
                              Width,bmpImageToConvert.Height,
                              GraphicsUnit.Pixel);
        gfxNewImage.Dispose();
        bmpImageToConvert.Dispose();

        /*bmpNewImage.Save(Server.MapPath("userData/" &
          Request.QueryString("ID") & "/" & e.Item.Cells(2).Text &
          ".jpg"),ImageFormat.Jpeg)
         */

        bmpNewImage.Save("images/sample3t.png", ImageFormat.Png);
    }
}

Thanks to :

http://www.codeverge.net/ng.asp-net-forum.getting_started/converting-a-png-or-gif-to-a-jpeg