Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public BitmapImage BitMapSourceToBitmapImage(BitmapSource bitmapSource)
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream memoryStream = new MemoryStream();
            BitmapImage bImg = new BitmapImage();

            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            encoder.Save(memoryStream);

            memoryStream.Position = 0;
            bImg.BeginInit();
            bImg.StreamSource = memoryStream;
            bImg.EndInit();
            return bImg;
        }

private System.Drawing.Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BmpBitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return new System.Drawing.Bitmap(bitmap);
            }
        }

public static Bitmap ConvertTo16bpp(Bitmap img)
        {
            var bmp = new Bitmap(img.Width, img.Height,
                          System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
            using (var gr = Graphics.FromImage(bmp))
                gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
            return bmp;
        }

private BitmapSource BitReduction(BitmapImage myBitmapImageFit)
        {
            FormatConvertedBitmap fcb = new FormatConvertedBitmap();
            fcb.BeginInit();
            fcb.Source = myBitmapImageFit as BitmapSource;
            fcb.DestinationFormat = System.Windows.Media.PixelFormats.Indexed4;
            fcb.DestinationPalette = BitmapPalettes.Halftone8;
            fcb.EndInit();
            return (fcb as BitmapSource);
        }


private void ApplyFilter(string path)
        {
            BitmapImage myBitmapImage = new BitmapImage();
            // BitmapSource objects like BitmapImage can only have their properties
            // changed within a BeginInit/EndInit block.
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(path);
            myBitmapImage.DecodePixelHeight = 240;
            myBitmapImage.DecodePixelWidth = 320;
            myBitmapImage.EndInit();

            var imageProcessed = ConvertTo16bpp(BitmapImage2Bitmap(myBitmapImage));
            var newbitmapimage = BitmapToBistmapImage(imageProcessed);
            BitmapSource bitmapSource = BitReduction(newbitmapimage);
            var bitmapToProcess = 
             BitmapImage2Bitmap(BitMapSourceToBitmapImage(bitmapSource));
            
            bitmapToProcess.Save(@"C:\image6.bmp");


byte[] byteArray = new byte[350000];

byteArray = File.ReadAllBytes(@"C:\image6.bmp");

List<string> result = new List<string> 
             (System.Text.RegularExpressions.Regex.Split(hello2, @"(?<=\G.{2})", 
              System.Text.RegularExpressions.RegexOptions.Singleline));
              string newstring = string.Join(" ", result);


What I have tried:

Steps:

-dither to 16 bit color 565
-reduce colors (4 bits per pixel, 16 colors)
-save the file
-get byte array from bmp file
-convert bytearray to hex string to test
So:
I would like to know if dithering method and bitreduction are doing the right work or must be improved and how?
Posted
Updated 7-Aug-19 5:03am

1 solution

Quote:
How do I improve my dither to 16 bit color 565 and reduce colors(16 colors)

What is the problem? don't works? bad quality result? too slow? ...
I don't use images libraries on a daily basis.

You dumped 8 blocks of code, and not a single comment explaining what is what.

You explain what result you want, but what is the starting point?
- My original image is ... size ... color encoded
- I do this operation to ... size ... color encoded
- I do this operation to ... size ... color encoded
- I get result as ... size ... color encoded

C#
bitmapToProcess.Save(@"C:\image6.bmp");

Storing file on root of C: is a bad idea because windows is actively acting against it.
Use a subdirectory/folder instead.

Note that I have not seen any dithering operation in this code.

[Update]
My procedure would be:
- Read image to variable/object
- Extend color scheme to 24 bits
- Resize to 240*320
- dither to the 16 colors of 4 bits palette
- convert to 4 bits color scheme
- save new image
 
Share this answer
 
v2
Comments
abdel hayan 8-Aug-19 2:41am    
My original image is 151 KB (bmp file)
it has 128 colors
(I give you just an example but the image could be of any size and maybe more colors and the same for the hight and width)
But the result must be such as:
1-width= 320 height = 240;

2- image dither to 16 color 565
3- reduce colors (4 bits per pixel, 16 colors)
4- save new image as bmp file
5- convert it to byte array

I thought with this method ConvertTo16bpp(Bitmap img) I am doing the second step
and with the method BitReduction the third step
abdel hayan 8-Aug-19 3:27am    
I will take saving the file in a subdirectory into account.
Thanks a lot
Merci beaucoup

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900