convert image 32BPP --> 24BPP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: convert image 32BPP --> 24BPP

  1. #1
    Join Date
    Apr 2002
    Posts
    29

    Unhappy convert image 32BPP --> 24BPP

    salute...

    My Head is burning... How do i convert a Image from the pixelformat 32BPP into 24BPP?

    If anyone knows how i can convert the Image to 8Bit Grayscale so all of my Problems are solved. But for now if i can convert an image to 24BPP im glad to.

    This is my Target Pixelformat
    System.Drawing.Imaging.BitmapData.PixelFormat.Format24bppRgb;

    And the could be using other strange Pixelformats.

    thanks.... Oliver

  2. #2
    Join Date
    Apr 2002
    Posts
    29
    Is there none, which must convert pictures? (image or bitmaps)

    bye
    Oliver

  3. #3
    Join Date
    Apr 2002
    Posts
    29
    I solved my problem with a small workaround... If anyone got a smaller and better solution, let me know.

    greets
    Oliver

    Code:
    private bool Save_Image_As_24BitGrayscale( Image SaveImage, string Filename )
    		{
    			// Variablen init
    			int			iLoopX, iLoopZ	= 0;					// Loop
    			Bitmap			mBMP, mSaveBMP;						// Bitmap
    			System.Drawing.Color	mPixel;							// Farbe
    			int			iSize			= SaveImage.Size.Height;	// Grösse
    			
    			// Bitmaps erstellen
    			mBMP		= new Bitmap( SaveImage );
    			mSaveBMP	= new Bitmap( iSize, iSize, System.Drawing.Imaging.PixelFormat.Format24bppRgb );
    						
    			// Jetzt loopen wir durch das Image
    			for( iLoopZ = 0; iLoopZ < iSize; iLoopZ++ )
    				for( iLoopX = 0; iLoopX < iSize; iLoopX++ )
    				{
    					// Pixel auslesen
    					mPixel = mBMP.GetPixel( iLoopX, iLoopZ );
    					
    					// Pixel schreiben
    					mSaveBMP.SetPixel( iLoopX, iLoopZ, mPixel );
    				}
    			
    			// Speichern des Bitmaps
    			mSaveBMP.Save( Filename, System.Drawing.Imaging.ImageFormat.Bmp );
    
    			// und wech
    			return true;
    		}

  4. #4
    Join Date
    May 2003
    Location
    Slovenia
    Posts
    6
    I was working on the same problem and found this solution:

    Bitmap pic1 = new Bitmap(pic.Width, slika.Height, PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage (pic1);
    g.DrawImage (pic, 0,0);
    g.Dispose ();

    pic = pic1;

    It works for me !!!



    To Grayscale:

    public Bitmap nastaviVSivine(Bitmap b)
    {
    if (b.PixelFormat != PixelFormat.Format24bppRgb)
    {
    return b;
    }

    bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    ptr = bmData.Scan0;
    nOffset = bmData.Stride- b.Width* 3;

    for (int y=0; y <= b.Height- 1; y++)
    {
    for (int x=0; x <= b.Width- 1; x++)
    {
    Blue = Marshal.ReadByte(ptr, 0);
    Green = Marshal.ReadByte(ptr, 1);
    Red = Marshal.ReadByte(ptr, 2);
    bVal = System.Convert.ToByte(0.299 * Red + 0.587 * Green + 0.114 * Blue);
    Marshal.WriteByte(ptr, 0, bVal);
    Marshal.WriteByte(ptr, 1, bVal);
    Marshal.WriteByte(ptr, 2, bVal);

    ptr = (IntPtr) (ptr.ToInt32()+3);
    }
    ptr = (IntPtr) (ptr.ToInt32() + nOffset);
    }
    b.UnlockBits(bmData);
    return b;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center