How do I copy DirectX texture to a surface? - winapi

I'm trying to capture whole desktop screen (front buffer) and add a logo and caption to each frame.
I load the logo (.png or .jpeg file) as IDirect3DTexture9 and I try to add it to a IDirect3DSurface9 image frame (screenshot).
As I'm new to DirecX9, I have no idea how to copy the logo (texture) to the screenshot (surface/buffer). Any help would be appreciated.
(If there's any other way to add logo to each frame without involving texture, please do tell me.)
EDIT:
I have used the code suggested in an answer below. The hr result returned is an error.
IDirect3DSurface9 *pSurface = NULL;
pDevice->GetFrontBufferData(0, pSurface); //my screenshot
LPDIRECT3DTEXTURE9 tex = NULL; //my logo
//[code to load logo from file here]
IDirect3DSurface9 *pSurf = NULL;
tex->GetSurfaceLevel(0, &pSurf);
hr = pDevice->StretchRect(pSurf, NULL, pSurface, NULL, D3DTEXF_NONE); //HR GIVES AN ERROR

You can use StretchRect.
The code will look something like this (pseudo):
ID3DSurface9 *pScreenSurface = ... // your screenshot surface should be created in default pool and the same format as your texture(see bellow), like this:
CreateOffscreenPlainSurface(width, height, D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, &pScreenSurface, NULL);
// Now get a screenshot by using either GetFrontBufferData, or GetBackBuffer/GetRenderTargetData by supplying pScreenSurface as a parameter (not shown here).
ID3DTexture9 *pTexture = ... // your texture should be loaded in default pool and the same format as your screenshot surface, like this:
D3DXCreateTextureFromFileEx(*ppDevice, L"icon.bmp", 40, 40, D3DX_DEFAULT, 0,
D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL,
NULL, &pTexture);
ID3DSurface9 *pSurf;
pTexture->GetSurfaceLevel(0, &pSurf); // don't forget to Release pSurf after StretchRect
RECT rc = { ... initialize the destination rectangle here };
pDevice->StretchRect(pSurf, NULL, pScreenSurface, &rc);
You need to specify a destination rectangle inside the destination surface, where you want your texture to be copied.

Related

How to use Itext to add an image file into an existing PDF, and declare a unique name for it?

In order to find an added image file and replace it with another image file when I read a PDF next time, I want to use Itext to add an image file into an existing PDF, and declare a unique name for it.
My code:
final PdfName key = new PdfName("MY_SIGN_KEY");
final PdfName val = new PdfName("MY_SIGN_VAL");
Image signImage=Image.getInstance(signPngFile.getAbsolutePath());
signImage.setAlignment(1);
signImage.scaleAbsolute(newWidth, newHeight);
signImage.setAbsolutePosition(200,200);
PdfContentByte over = stamper.getOverContent(1);
PdfImage stream = new PdfImage(signImage, "", null);
stream.put(key,val);// a unique name for it.(设置唯一标识符)
//PdfIndirectObject ref=over.getPdfWriter().addToBody(stream);
//signImage.setDirectReference(ref.getIndirectReference());
over.addImage(signImage);
I have tried your code and it works for me. See the AddImageWithID example:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Image image = Image.getInstance(IMG);
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 400);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
}
In this example, I take a file named hello.pdf and I add an image named bruno.jpg with the file hello_with_image_id.pdf as result.
The image doesn't look black:
The ID is added:
Can you try the code I shared and see if the problem persists.
I can think of one reason why you'd get a black image: in our code, we assume that a single image is added. In the case of JPEG, this is always the case. In the case of PNG or GIF though, adding one source image could result in two images being added. Strictly speaking, PDF doesn't support transparent images (depending on how you interpret the concept of transparent images). Whenever you add a single source image with transparent parts, two images will be added to the PDF: one opaque image and one image mask. The combination of the opaque image and the image mask results in something that is perceived as a transparent image. Maybe this is what happens in your case.

Save high quality images from Autocad

my problem is that I don´t have much experience with Autocad. Thus I don´t know how to save a project in a good quality image (png?) to insert in a latex document.
Could you give me a hint?
Thank you
Autocad's Publish to Web printers are pretty bad. What I would do is print using DWG to PDF printer or similar (there are a few in autocad's default printer list) then convert that pdf to raster images using a second software like Photoshop, GIMP, etc. There are even small software that convert pdf's to jpgs like TTRPDFToJPG3. If you have a specific idea of what kind of output you're looking for, please feel free to elaborate further. cheers!
If you're looking for a programmatic way to capture the screen, here it is:
using acApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System.Drawing.Imaging;
using System.Drawing;
namespace ScreenshotTest
{
public class Commands
{
[CommandMethod("CSS")]
static public void CaptureScreenShot()
{
ScreenShotToFile(
acApp.Application.MainWindow,
"c:\\main-window.png",
0, 0, 0, 0
);
ScreenShotToFile(
acApp.Application.DocumentManager.MdiActiveDocument.Window,
"c:\\doc-window.png",
30, 26, 10, 10
);
}
private static void ScreenShotToFile(
Autodesk.AutoCAD.Windows.Window wd,
string filename,
int top, int bottom, int left, int right
)
{
Point pt = wd.Location;
Size sz = wd.Size;
pt.X += left;
pt.Y += top;
sz.Height -= top + bottom;
sz.Width -= left + right;
// Set the bitmap object to the size of the screen
Bitmap bmp =
new Bitmap(
sz.Width,
sz.Height,
PixelFormat.Format32bppArgb
);
using (bmp)
{
// Create a graphics object from the bitmap
using (Graphics gfx = Graphics.FromImage(bmp))
{
// Take a screenshot of our window
gfx.CopyFromScreen(
pt.X, pt.Y, 0,0, sz,
CopyPixelOperation.SourceCopy
);
// Save the screenshot to the specified location
bmp.Save(filename, ImageFormat.Png);
}
}
}
}
}
Source: Taking screenshots of AutoCAD’s main and drawing windows using .NET
Thanks to everyone. I am saving the files in pdf and after I´m using GIMP to convert them in PNG.

I want to display numbers on the system tray notification Icons on windows

I am trying to create a notification Icon based application in which I want to display some numbers ranging from 1-999.
I looked at this video which is similar to what I want to do but here the system tray icon just displays the icon and it shows a pop up rather than the system tray icon showing the number or any text.
Excluding the popup item, all I want to do is to read a number (input from somewhere) and display that number in the notification icon section.
I am open to trying any technology (QT, .net) for doing this. Basically, I am looking for some examples.
While parts of your question are vague, this is very possible, I'd even dare-say quite simple. Since you mentioned you're open to trying any technology, C# would probably simplify things for you.
Generate a new 16 x 16 Bitmap and draw the number to it using the Graphics class.
Convert the Image instance to an Icon instance, after disposing of your Graphics object.
Set the Icon property of your NotifyIcon to the icon you've just created.
These are the basic steps. You'll likely need to do some research if you aren't familiar with the classes used.
Thanks for replying to my question. Here is what I came up with. Not sure if this is what you were talking about.
Bitmap bmp = new Bitmap(WindowsFormsApplication2.Properties.Resources._16by16BitmapIcon);
RectangleF rectf = new RectangleF(2, 2, 16, 16);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("99", new Font("Tahoma", 7), Brushes.Blue, rectf);
pictureBox1.Image = bmp;
pictureBox1.Height = bmp.Height;
pictureBox1.Width = bmp.Width;
g.Dispose();
var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero);
thumb.MakeTransparent();
notifyIcon1.Icon = Icon.FromHandle(thumb.GetHicon());
Now my next question could this be done in a better way? This is my first C Sharp app so any suggestions are welcome!
public void ShowText(string text, Font font, Color col)
{
Brush brush = new SolidBrush(col);
// Create a bitmap and draw text on it
Bitmap bitmap = new Bitmap(16, 16);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(text, font, brush, 0, 0);
// Convert the bitmap with text to an Icon
Icon icon = Icon.FromHandle(bitmap.GetHicon());
m_notifyIcon.Icon = icon;
}

Cant add bmp file to picture control

I'm trying to add a bmp picture in picture controle in my dialog based app.
So basically I have added picture control in dialog, and I have changed picture control type from frame to bitmap. Picture control ID is IDC_STATIC
After that I added this code in header:
CStatic* m_picture;
In project file under OnInitDialog I added this:
m_picture = (CStatic *)GetDlgItem(IDC_STATIC);
HITMAP hb = (HBITMAP)::LoadImage(NULL, L"C:\1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
m_picture->ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE);
m_picture->SetBitmap(hb);
I don't get any errors, however the app doesn't work, it doesn't display anything...
Am I missing something?
I think Your Path is not relevant
TO add Image to a Picture Control
// Declare CStatic Object
CStatic aDummyImageName;
//On InitFunction
BOOL YourClassName::OnInitDialog()
{
aDummyImageName= (CStatic*)GetDlgItem(ID of Your Picture Control);
HBITMAP dateImage = (HBITMAP)LoadImage(NULL, L"Path of the Image", IMAGE_BITMAP, 225, 170, LR_LOADFROMFILE);
aDummyImageName->SetBitmap(dateImage);
return TRUE;
}
Try This, It Will Work
IDC_STATIC is used for everything that doesn't change. The program can't find it with that. But once you fix that you may have MY problem, which is that Visual studio keeps revising my picture control in *.rc to no longer be "static", and then it won't build. :(

Capturing a Window that is hidden or minimized

I followed this tutorial (there's a bit more than what's listed here because in my code I get a window via mouse click) for grabbing a window as a bitmap and then rendering that bitmap in a different window.
My question:
When that window is minimized or hidden (SW_HIDE) my screen capture doesn't work, so is it possible to capture a window when it is minimized or hidden?
The PrintWindow api works well, I use it for capturing thumbnails for hidden windows. Despite the name, it is different than WM_PRINT and WM_PRINTCLIENT, it works with pretty much every window except for Direct X / WPF windows.
I added some code (C#) but after reviewing how I used the code, I realized that the window isn't actually hidden when I capture its bitmap, its just off screen so this may not work for your case. Could you show the window off screen, do a print and then hide it again?
public static Bitmap PrintWindow(IntPtr hwnd)
{
RECT rc;
WinUserApi.GetWindowRect(hwnd, out rc);
Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap = gfxBmp.GetHdc();
bool succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
if (!succeeded)
{
gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
}
IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0);
WinUserApi.GetWindowRgn(hwnd, hRgn);
Region region = Region.FromHrgn(hRgn);
if (!region.IsEmpty(gfxBmp))
{
gfxBmp.ExcludeClip(region);
gfxBmp.Clear(Color.Transparent);
}
gfxBmp.Dispose();
return bmp;
}
There are WM_PRINT and WM_PRINTCLIENT messages you can send to the window, which cause its contents to be rendered into the HDC of your choice.
However, these aren't perfect: while the standard Win32 controls handle these correctly, any custom controls in the app might not.
I am trying to get the bitmap of partially hidden controls.
I used code before that did the drawing, but included windows overlapping it. So.. maybe you want to try this.
The WM_PRINTCLIENT should (in my understanding) redraw all inside the control, even if it is not really visible.
const int WM_PRINT = 0x317, WM_PRINTCLIENT = 0x318, PRF_CLIENT = 4,
PRF_CHILDREN = 0x10, PRF_NON_CLIENT = 2,
COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT;
SendMessage(handle, WM_PRINTCLIENT, (int)hdc, COMBINED_PRINTFLAGS);
//GDIStuff.BitBlt(hdc, 0, 0, width, height, hdcControl, 0, 0, (int)GDIStuff.TernaryRasterOperations.SRCCOPY);
The before code is commented out now. It is based on the code found here: Pocket PC: Draw control to bitmap (accepted answer). It is basically the same as Tim Robinson suggests in this thread.
Also, have a look here
http://www.tcx.be/blog/2004/paint-control-onto-graphics/

Resources