How to save bmp file from ID2D1Bitmap

限于喜欢 提交于 2020-01-16 11:10:06

问题


I am trying to create the bmp file from the live running video using Kinect. I am developing an application which is running the live video on top of that to place an image. The IDE which I am used is Visual Studio Professional 2010. The code I am developing in C++ using win32.
. I want to save the video along with the overlayed image. Now I am using ID2D1Bitmap for displaying the bitmap in overlayed manner. But I have to retrieve the byte* data from the video with overlayed image. I am trying to create a bmp file which retrieve the byte* data from the ID2D1Bitmap. But the direct conversion does not possible to retrieve the byte* data.

            m_pd2d1RenderTarget->DrawBitmap(m_pd2d1Bitmap_Image,D2D1::Rect(120,140, 120+Height,140+Width));
            m_pd2d1RenderTarget->DrawBitmap(m_pd2d1Bitmap_Video);

  I copied the data to the ID2D1Bitmap using the function called,
           m_pd2d1RenderTarget->CopyFromMemory(NULL,pvideo_Data,video_Info.bmWidthBytes); m_pd2d1RenderTarget->CopyFromMemory(NULL, pBitmap_Data,Bitmap_Info.bmWidthBytes);

But Now I want to combine the both bitmaps and to get the byte* data of that. Is it possible to convert those bitmaps into byte*? Is it any direct conversion available to get the byte* data from ID2D1Bitmap???. And also I am tried to convert the ID2D1Bitmap to IWICBitmap. Because with using IWICBitmap->Lock can retrieve the byte* content. So please help me to the following doubts and give the valuable guidance:

1.Conversion of ID2D1Bitmap to byte*?.

2.How to convert ID2D1Bitmap to IWICBitmap?.

Thanks in advance :)

Regards,

Vvk.


回答1:


I did this in C# as follows. The code we wrote captures the byte array from the frame, creates an Image type, converts to a Bitmap type from the function below, then saves as a compressed jpeg for later saving on the filesystem:

using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
                {
                    if (depthFrame == null) return;
                    byte[] pixels = GenerateColoredBytes(depthFrame, first);

                    int stride = depthFrame.Width * 4;
                    depthImage.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);

                    Bitmap btmap = GetBitmap((BitmapSource)depthImage.Source);

                    // Encoder parameter for image quality
                    long quality = 100000;
                    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                    // Jpeg image codec
                    ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = qualityParam;

                    var stream = new MemoryStream(); // Create MemoryStream
                    btmap.Save(stream, jpegCodec, encoderParams); //(stream, ImageFormat.Jpeg); // Save Bitmap as .jpeg in MemoryStream                    

                    depthPath = "_UserTrialImages/" + UsernameInput.username + "/Date-" + DateTime.Now.ToString("MM.dd.yyyy");
                    gamePath = depthPath + "/Game-" + gameNumber;
                    trajectoryPath = gamePath + "/Trajectory-" + trajectoryNumber;     // one trajectory per super bubble appearance

                    string depthFile = "image" + ind[5] + ind[4] + ind[3] + ind[2] + ind[1] + ind[0] + ".jpg";
                    string pathFile = trajectoryPath + '/' + depthFile;

                    imageNumberCounter();
                    newImg.pathFile = pathFile;
                    newImg.stream = stream;

                    // saves depth images in list
                    listOfImages.Add(newImg);
                }

which calls this function:

Bitmap GetBitmap(BitmapSource source)
{
    Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    BitmapData data = bmp.LockBits(
      new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
      ImageLockMode.WriteOnly,
      System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    source.CopyPixels(
      Int32Rect.Empty,
      data.Scan0,
      data.Height * data.Stride,
      data.Stride);

    bmp.UnlockBits(data);

    return bmp;
}

I do not have this in C++, but if you are coding in .NET 4.0, there should be equivalent functions that will do the same as the above in C++. Hope this helps.



来源:https://stackoverflow.com/questions/14725453/how-to-save-bmp-file-from-id2d1bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!