|
Hi Everyone,
I did a search on capturing screen shots in vb.net and I was wondering if there is a straight forward way to capture the active window? Or do I need to first get the handle of the active window, get the position and size and then capture it? I do not want to use any type of sendkeys method or use the clipboard. I want to save it to jpg format.
Thanks!
Done so far but its capturing the entire window is
Module patternAload
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Integer
Const SRCCOPY As Integer = &HCC0020
Private oBackground As Bitmap
Private FW, FH As Integer
Public Sub CaptureScreen()
Dim hSDC, hMDC As Integer
Dim hBMP, hBMPOld As Integer
Dim r As Integer
Dim fgw As Long = GetForegroundWindow()
hSDC = CreateDC("DISPLAY", "", "", "")
hMDC = CreateCompatibleDC(hSDC)
FW = GetDeviceCaps(hSDC, 8)
FH = GetDeviceCaps(hSDC, 10)
hBMP = CreateCompatibleBitmap(hSDC, FW, FH)
hBMPOld = SelectObject(hMDC, hBMP)
r = BitBlt(hMDC, 40, 40, FW, FH, hSDC, 0, 0, SRCCOPY)
hBMP = SelectObject(hMDC, hBMPOld)
r = DeleteDC(hSDC)
r = DeleteDC(hMDC)
oBackground = Image.FromHbitmap(New IntPtr(hBMP))
DeleteObject(hBMP)
End Sub
|
|
|
|
|
I suspect that the following code should help:
<DllImport("user32.dll")> _
Public Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", ExactSpelling := True, CharSet := CharSet.Auto)> _
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Public Shared Sub CaptureWindow(fileName As String, format As ImageFormat)
Dim hWnd As IntPtr = GetForegroundWindow()
If hWnd = IntPtr.Zero Then
Return
End If
Dim srcRect As RECT
If Not GetWindowRect(hWnd, srcRect) Then
Return
End If
Dim width As Integer = srcRect.Right - srcRect.Left
Dim height As Integer = srcRect.Bottom - srcRect.Top
Using bmp As New Bitmap(width, height)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(srcRect.Left, srcRect.Top, 0, 0, New Size(width, height), CopyPixelOperation.SourceCopy)
bmp.Save(fileName, format)
End Using
End Using
End Sub If you want to do it in C#, the same code would look like this:
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static void CaptureWindow(string fileName, ImageFormat format)
{
IntPtr hWnd = GetForegroundWindow();
if (hWnd == IntPtr.Zero) return;
RECT srcRect;
if (!GetWindowRect(hWnd, out srcRect)) return;
int width = srcRect.Right - srcRect.Left;
int height = srcRect.Bottom - srcRect.Top;
using (Bitmap bmp = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(srcRect.Left, srcRect.Top, 0, 0,
new Size(width, height), CopyPixelOperation.SourceCopy);
bmp.Save(fileName, format);
}
}
}
|
|
|
|
|
OK i will retry with this code lets see what happens ..
Thanks
|
|
|
|
|
|
i got confused sorry ... so the public sub to save the file will be ???
i try Private Sub Form1_load()
CaptureWindow(fileName:="C:\test.jpg", format:="")
bmp.Save("C:\test.jpg")
End Sub
and its not working
|
|
|
|
|
hey my bad im sorry =)
i got it
bmp.Save("test.bmp", jpeg)
i did change it and it wasnt working i had to put
bmp.save("c:\test.bmp", system.drawing.imaging.ImageFormat.Jpeg)
and on the click
CaptureWindow("C:\test.bmp", Jpeg)
that was it
Thanks
|
|
|
|
|
Sweet. Not too bad when you consider I just knocked that up in Notepad. I'm glad it's working for you.
|
|
|
|
|
hehe Thanks a lot man
i have another question its killing me lol ima post the new topic =) is different lol .. thanks a gain man
|
|
|
|
|
I have an application that uses Comparer class to compare the objects. In 4.0 framework, the function public int Compare(object a, object b) took much time than 2.0. Please help me to resolve this issue!!!
modified 8-Aug-12 8:32am.
|
|
|
|
|
Could you show us the code you were using here? That might help to narrow things down.
|
|
|
|
|
Thanks for your reply. When i change the Target FrameWork from 4 to 2 in Project Properties in VS 10, it took less time.
Please find the below code snippet:
public static int CompareKeyColumn(DataRow drSource, DataRow drTarget, ArrayList arrForeignKey)
{
int iComareVal = 0;
string strColumnName = "";
Comparer cRow = new Comparer(new CultureInfo("es-ES", false));
for (int j = 0; j < arrForeignKey.Count; j++)
{
strColumnName = arrForeignKey[j].ToString();
if (drSource[strColumnName] != DBNull.Value && drTarget[strColumnName] != DBNull.Value)
{
iComareVal = cRow.Compare(drSource[strColumnName], drTarget[strColumnName]);
}
else if (drSource[strColumnName] == DBNull.Value)
iComareVal = -1;
else if (drTarget[strColumnName] == DBNull.Value)
iComareVal = 1;
if (iComareVal > 0)
return 1;
else if (iComareVal < 0)
return -1;
}
return 0;
}
|
|
|
|
|
My suspicion is that the problem actually lies with the culture. This is what happens inside Compare:
public int Compare(object a, object b)
{
if (a == b)
{
return 0;
}
if (a == null)
{
return -1;
}
if (b == null)
{
return 1;
}
if (this.m_compareInfo != null)
{
string str = a as string;
string str2 = b as string;
if ((str != null) && (str2 != null))
{
return this.m_compareInfo.Compare(str, str2);
}
}
IComparable comparable = a as IComparable;
if (comparable == null)
{
throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
}
return comparable.CompareTo(b);
} To test this hypothesis out, try dropping the CultureInfo out and see what happens. (The m_compareInfo.Compare part is the test that occurs when you set the CultureInfo ).
|
|
|
|
|
Sorry! I didnt get your answer. How to drop the CultureInfo? May i use Comparer.Default.Compare(object a, object b) function?
|
|
|
|
|
To test it, change
Comparer cRow = new Comparer(new CultureInfo("es-ES", false)); to
Comparer cRow = new Comparer();
|
|
|
|
|
I couldnt do it. The Comparer Constructor must take CultureInfo as Parameter. Default constructor is not available!!!
|
|
|
|
|
Doh. Of course it isn't. Sorry about that. OK, use the Comparer.Default.Compare(object, object) method to test the timings.
|
|
|
|
|
Ok. I will try and let you know
|
|
|
|
|
ruby_e_s wrote: I will try and let you know
Looking forward to seeing it.
|
|
|
|
|
Yeah! Now Framework 4 also took less time!!! Thanks for your help...
|
|
|
|
|
Not a problem. I'm glad I could help.
|
|
|
|
|
how to draw ecg waveform in wpf please anyone give tips...advance in thanks whomever helping me...thanks friends...
|
|
|
|
|
Follow one of the tutorials[^] out there.
You're welcome.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
You should ask on the "Silverlight / WPF" forum. That's where the WPF gurus hang out.
That being said, start by looking at the Canvas control, and Line element.
|
|
|
|
|
i want in wpf please help me if u can
|
|
|
|
|
Canvas is in System.Windows.Controls and Line is in System.Windows.Shapes . Also look at System.Windows.Shapes.Polyline .
These are WPF .
|
|
|
|