C # convert Raw (8-bit or 10-bit or 12-bit or 14-bit or 16-bit image) Bmp image

I have Raw Images files (Raw8 or Raw10 or Raw12 or Raw14 or Raw16 …). I want to convert Raw to BMP.

The code works but I still have problems with the results.

public void ReadImage (out byte[] Data, String Path, uint Width, uint Height, uint BPP) {
byte[] fileData = null;
if (! File.Exists (path))
launch a new FileNotFoundException (path);

fileData = File.ReadAllBytes (path);
var bytesPerPixel = (BPP + 7) / 8;
var dataSize = Width * Height * bytesPerPixel;
Data = new byte[Width * Height * bytesPerPixel];
var sdata = new short[dataSize / 2];
if (bytesPerPixel == 1)
for (int i = 0, shortIndex = 0; i <data size, i + = 2, shortIndex ++)
CopyBytesToShort (fileData[i], fileData[i + 1], excluding data[shortIndex], (int) BPP, false);
if (bytesPerPixel == 2)
for (int i = 0, shortIndex = 0; i <data size, i + = 2, shortIndex ++)
CopyBytesToShort (fileData[i], fileData[i + 1], excluding data[shortIndex], (int) BPP, true);
Buffer.BlockCopy (sdata, 0, Data, 0, Data.Length);
int w = Convert.ToInt32 (Width);
int h = Convert.ToInt32 (Height);
fixed (byte * ptr = Data)
using (var bmp = new Bitmap (w / 4, h / 4, w * 4, PixelFormat.Format32bppRgb, new IntPtr (ptr)))

Bitmap dmybp = new Bitmap (bmp, new size (w, h));
dmybp.Save (@ "RAW.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
private void CopyBytesToShort (octet byte1, byte byte2, short retshort, int bitsPerPixel, bool isPerformShift)
short lsb, msb;
lsb = octet1;
msb = byte2;
if (isPerformShift)
lsb << = 16 - bitsPerPixel;
msb << = (24 bitsPerPixel);
msb << = 8;
retShort = (short) (msb | lsb);

if I run with Raw8 can get a correct picture.enter the description of the image here

But when I run another RAW file (Raw10 or Raw12 or Raw14 or Raw16 …), I have a failure image.
Like that:
enter the description of the image here

Please comment on the optimization of my code in C # and on the best way to convert an image.

c ++ – how to find the 16 most common colors in img with BMP extension

Some of your past responses have not been well received and you may be stuck.

Please pay close attention to the following tips:

  • Please make sure to respond to the question. Provide details and share your research!

But to avoid

  • Ask for help, clarification, or answer other answers.
  • Make statements based on opinions; save them with references or personal experience.

To learn more, read our tips for writing good answers.

How to display BMP images in a simple Java image viewer

I am newer in programming and try to make my image viewing program display BMP images. The program works for JPEG and PNG images but not for BMP. I commented on the attempts I made for BMP to work, which I found online. The methods I found online all seem to convert the BMP file to a PNG file. I would prefer that the image remains a BMP file, but if it is not possible, I would still like to make it work where it is converted to PNG. In addition, I know how to change the size of the image window but I do not know what determines the display size of the image or if there is a way to change that. I've included what I've got up to here below:

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event. *;
import java.io. *;
import javax.swing. *;
import javax.imageio.ImageIO;

Public class ImageViewer {

public static void main (String[] args) {
JFrame frame = new ImageViewerFrame ();
frame.setTitle ("Image Viewer");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);

the ImageViewerFrame class extends JFrame {
JLabel label;
JFileChooser selector;
JMenuBar menu bar;
JMenu menu;
JMenuItem menuitem;

public ImageViewerFrame () {

setSize (500 500); // set the size of the window

label = new JLabel ();
add (label);

chooser = new JFileChooser ();
chooser.setCurrentDirectory (new file ("."));

menu bar = new JMenuBar ();
setJMenuBar (menu bar);

menu = new JMenu ("File");
menubar.add (menu);

menuitem = new JMenuItem ("Open");
menu.add (menuitem);

menuitem.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
int result = chooser.showOpenDialog (null);
if (result == JFileChooser.APPROVE_OPTION) {

// Image image = ImageIO.read

String name = chooser.getSelectedFile (). GetPath ();

// Image image = ImageIO.read (name);
// ImageIcon icon = new ImageIcon (name);
// JLabel test = new JLabel (icon);

label.setIcon (new ImageIcon (name)); // open the file

// ImageIcon icon = new ImageIcon (name);
// label.setIcon (new ImageIcon (icon));


How to convert a BMP, PNG or JPG image to PDF?

Find an online PDF creator designed to convert various documents to PDF.
online2pdf is my favorite free online service for saving images in PDF format. This is a free platform that allows users to convert Word, Excel, PowerPoint, IMAGE, Text, etc. into PDF format, even if you can merge, compress and protect the PDF output while you are there. saving these files in PDF format. In addition, users can export PDFs to Word, Excel, PowerPoint and images.