Render bitmap to SpriteBatch in DirectXTK - graphics

I need to port some C++ application to Windows Phone 8 (it is already on Android, iOS, WinCE and Win32). Currently I need to solving how to display graphic. I can get rendered bitmap from core application and I after succesfully initialize DirectXTK I'm able to render some DDS texture (DirectXTK::SpriteBatch). Now I need to transform my bitmap to texture and then render it. Can you help me with this out? Or is there some way to put bitmap directly to backbuffer and show it on display without SpriteBatch?
Thank you very much
Tomas

DirectX toolkit has WICTextureLoader. You can use it instead of DDSTextureLoader for loading .bmp(bitmap) file. Hope this help!
http://directxtk.codeplex.com/wikipage?title=WICTextureLoader&referringTitle=DirectXTK

Since WICTextureLoader is not supported on Windows Phone 8 the only way to render a bitmap to a texture is by mapping the texture to the CPU and copy your bitmap resource onto the mapped texture's resource.
ID3D11DeviceContext::Map()
http://msdn.microsoft.com/en-us/library/windows/desktop/ff476457(v=vs.85).aspx
D3D11_MAPPED_SUBRESOURCE mappedBuffer;
HRESULT hr = pContext->Map(pTexture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedBuffer);
if(hr == S_OK)
{
// copy your bitmap onto mappedBuffer.pData
...
pContext->Unmap(pTex, 0);
}

Related

Loading a .bmp file and rendering it as the background in DirectX 11

I have spent the afternoon looking over the documentation on the contexts / surfaces and followed quite a few guides but I just do not understand how this is done.
All I want is to use a bitmap (already loaded) and to put it into my scene as the background.
I heard that I have to use a surface and draw it first but I have absolutely no idea how to obtain the surface or how to assign the bitmap to it.
Any help is appreciated.
Yes one method is to use Surface, however I would recommend this method
I am not sure how you have loaded bitmap, anyhow you can use bitmap as background in this way
//Make texture object
LPDIRECT3DTEXTURE9 m_myBitmapTexture;
// During Initialization, Load texture from file
if(FAILED(D3DXCREATETEXTUREFROMFILE(device,"filepath\\file.bmp", 0, 0, 0, D3DMFT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFULT, D3DX_DEFAULT, 0x00000000, NULL, NULL, *m_myBitmapTexture)))
return E_FAIL;
// During Rendering, set texture
device->SetTexture(0, m_myBitmapTexture);
device->SetStreamSource(0, yourBuffer, 0, size(YourBufferStruct));
device->SetFVF(yourTextureFVF); // Setting flexible vertex format
device->DrawPrimitive(topologyType, startindex, totalIndex);
You just need to make sure, your buffer should have texture coordinates and your shader too
struct YourBufferStruct
{
D3DXVECTOR3 position;
D3DXVECTOR2 textureCoord;
}
// Define your flexible vertex format, i am just adding position and texture,
//well you can add color, normal whatever extra you want
#define yourTextureFVF (D3DFVF_XYZ | D3DFVF_TEX1)
Now add texture coordinates to shader too
For more details you can consult this link https://msdn.microsoft.com/en-us/library/windows/desktop/bb153262(v=vs.85).aspx

How to have code that prints to a Device Context save the printout to a BMP file

I am struggling to adapt some code that has printing support to DCs (Device Contexts) and having it print to a Bitmap that can then be saved to disk.
I have tried the following code:
// The include files needed for my code
#include "atlimage.h"
#include "afxglobals.h"
// My attempt to get a DC which allows me to draw onto a CBitmap of A4 size
// (21.0x29.7 cm) in 300dpi, or 2480x3508 pixels in 24bit
HBITMAP BMP = CreateBitmap(2480,3508,1,24,NULL);
CBitmap* BitMap = CBitmap::FromHandle(BMP);
CMemDC A4(*CDC::FromHandle(CreateCompatibleDC(NULL)),CRect(0,0,1,1));
// This is where it fails. The SelectObject returns NULL, which means "failed"
// It may be caused by earlier code, but this is where I can detect the problem
// for the first time.
CBitmap* bmp = A4.GetDC().SelectObject(BitMap);
if (bmp == NULL)
TRACE("Error=%08X\n",GetLastError());
//
// The actual code that does the printing:
//
Graphs[0]->Print(A4.GetDC(),CRect(0,20,75,75));
Graphs[1]->Print(A4.GetDC(),CRect(75,20,100,75));
// And the saving to disk
CImage IMG;
IMG.Attach(*BitMap);
HRESULT res = IMG.Save("D:\\AUDIO.BMP");
IMG.Detach();
// Cleanup
DeleteObject(BMP);
DeleteDC(A4.GetDC());
TRACE("Result=%d\n",res);
The first TRACE prints out 00000000 as the error code, so I cannot tell (from this) why it fails.
I'd prefer not to use GDI+ if possible, but if GDI+ (which I don't have experience with) makes it much easier, I won't refuse it. I am using Visual Studio 2008 and cannot update to a newer version (it is part of a much bigger project that will take more time to upgrade to a newer version than I have time right now).
The caveat is that I only have printing routines that take a DC, and am not able to change this, so it must be done via a DC.
Any help would be appreciated...
Assuming that the error you refer to happens when you attempt to select the bitmap into the memory DC, the most likely explanation is that they are not compatible.
You have created a memory DC that is compatible with your desktop and that's most likely a 32bpp bitmap. Then you create a 24bpp bitmap, which I think is the problem.
To make sure everything is compatible, try this instead:
HDC hDC = CreateCompatibleDC(NULL);
HBITMAP BMP = CreateCompatibleBitmap(hDC, 2480,3508);
CBitmap* BitMap = CBitmap::FromHandle(BMP);
CMemDC A4(*CDC::FromHandle(hDC),CRect(0,0,1,1));
So, you create a bitmap and a DC that are compatible with the same original DC. Then the SelectObject should not fail.
Maybe update your question or comment on this answer if you don't get further.

OpenCL clCreateFromGLTexture using a different texture target

The aim of my project is to get live camera feed from on an Android device, use OpenCL to perform real-time filtering on those images and render the output on display.
I aim to do this in real-time that's why I am using OpenCL-OpenGL interop.
I have successfully managed to create a shared context using EGLContext and EGLDisplay. Now I am trying to use clCreateFromGLTexture so I can access these images in OpenCL kernel. The problem however is android requires that when bind the texture the target must be GL_TEXTURE_EXTERNAL_OES as it says here: (http://developer.android.com/reference/android/graphics/SurfaceTexture.html), however this texture target is not valid texture target when using clCreateFromGLTexture (https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateFromGLTexture2D.html).
So I am not sure how to go about this.
This is how I create a GL Texture in android:
GLES20.glGenTextures(1, texture_id, 0);
GLES20.glBindTexture(texture_target, texture_target);
and this is how I am trying to create a cl memory object:
glTexImage2D(texture_target, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
cl_mem camera_image = clCreateFromGLTexture(m_context, CL_MEM_READ_WRITE, texture_target, 0, texture_id, &err);
The error I get when I try to create cl memory object from GL texture is CL_INVALID_VALUE.
I am pretty new to OpenGL so there could be something basic I might have over looked.
The texture you receive from the camera is not the usual texture you'd expect. You even have to specify the extension in a shader if you read from it.
You need to perform an additional copy from the GL_TEXTURE_EXTERNAL_OES target to another texture which is created in the usual way. With luck you can bind both of them into fbo's and then just issue blit. If that doesn't work you can always use the normal texture as a rendertarget and simply draw a quad textured with the camera image.

GetBitmap() not working in release mode

I am Working in MFC application. I have to get Height and Width of a BITMAP image. Code I'm using is working in Debug mode only but because of some problem I have to use release mode and in Release mode code is not working.help me out..!!!
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
BITMAP bm;
bmp.GetBitmap(&bm);
CBitmap bmp;
Don't use a local variable to draw a bitmap. It will be gone after the function has been called.
Use a member variable.
For instance:
m_Background.LoadBitmap (IDB_BITMAP1);
BITMAP bm;
m_Background.GetBitmap (&bm);
m_BitmapSize = CSize (bm.bmWidth, bm.bmHeight);
Invalidate(1);

How do you resize a Bitmap under .NET CF 2.0

I have a Bitmap that I want to enlarge programatically to ~1.5x or 2x to its original size. Is there an easy way to do that under .NET CF 2.0?
One "normal" way would be to create a new Bitmap of the desired size, create a Graphics for it and then draw the old image onto it with Graphics.DrawImage(Point, Rectangle). Are any of those calls not available on the Compact Framework?
EDIT: Here's a short but complete app which works on the desktop:
using System;
using System.Drawing;
class Test
{
static void Main()
{
using (Image original = Image.FromFile("original.jpg"))
using (Bitmap bigger = new Bitmap(original.Width * 2,
original.Height * 2,
original.PixelFormat))
using (Graphics g = Graphics.FromImage(bigger))
{
g.DrawImage(original, new Rectangle(Point.Empty, bigger.Size));
bigger.Save("bigger.jpg");
}
}
}
Even though this works, there may well be better ways of doing it in terms of interpolation etc. If it works on the Compact Framework, it would at least give you a starting point.
The CF has access to the standard Graphics and Bitmap objects like the full framework.
Get the original image into a Bitmap
Create a new Bitmap of the desired size
Associate a Graphics object with the NEW Bitmap
Call g.DrawImage() with the old image and the overload to specify width/height
Dispose of things
Versions:
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0

Resources