Diskuze: Jak vytvořit a uložit screenshot formu (formuláře)
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.

Člen

Zobrazeno 8 zpráv z 8.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Ano, ale všechny jsou přes bitmapu \)nebo alespon ty, které jsem našel)
Chybu to vypisuje jasně.Musíš to tam dát celé:
System.Drawing.Imaging.ImageFormat.Jpeg
jinak ti to bere ImageFormat z prostoru OxyPlot
aha díky to jsem nevěděl, nicméně mi to nesnímá ten graf, jak to?
OxyPlot neznám, nemusí být podporován. Hledat se mě to nechce.
To je právě ono, ona ta knihovna nepodporuje bitmapu, proto by mě spíše jen zajímal obyčejný print screen, který třeba snadno udělám ve window zkratkou alt+print
Dolaď si to:
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WINGDISAMPLE
{
public partial class Form1: Form
{
string screenshotfileneme;
public Form1()
{
InitializeComponent();
screenshotfileneme = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "appscreenshot.png");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using(Bitmap bmp = SafeNativeMethods.CaptureWindow(this.Handle))
{
using(FileStream fs = new FileStream(screenshotfileneme, FileMode.Create, FileAccess.Write))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
internal static class SafeNativeMethods
{
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int nxDest, int nyDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int width, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
const int SRCCOPY = 0x00CC0020;
const int CAPTUREBLT = 0x40000000;
//
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//
static Bitmap CaptureRegion(Rectangle region)
{
IntPtr desktophWnd;
IntPtr desktopDc;
IntPtr memoryDc;
IntPtr bitmap;
IntPtr oldBitmap;
bool success;
Bitmap result;
desktophWnd = GetDesktopWindow();
desktopDc = GetWindowDC(desktophWnd);
memoryDc = CreateCompatibleDC(desktopDc);
bitmap = CreateCompatibleBitmap(desktopDc, region.Width, region.Height);
oldBitmap = SelectObject(memoryDc, bitmap);
success = BitBlt(memoryDc, 0, 0, region.Width, region.Height, desktopDc, region.Left, region.Top, SRCCOPY | CAPTUREBLT);
try
{
if(!success)
{
throw new Win32Exception();
}
result = Image.FromHbitmap(bitmap);
}
finally
{
SelectObject(memoryDc, oldBitmap);
DeleteObject(bitmap);
DeleteDC(memoryDc);
ReleaseDC(desktophWnd, desktopDc);
}
return result;
}
public static Bitmap CaptureWindow(IntPtr hWnd)
{
RECT region;
GetWindowRect(hWnd, out region);
return CaptureRegion(Rectangle.FromLTRB(region.left, region.top, region.right, region.bottom));
}
}
}
Zobrazeno 8 zpráv z 8.