unter WinForms wende ich zum Zeichnen u.a. die StretchBlt-Funktion an, um eine Grafik möglichst schnell zu skalieren:
void DrawBitmap(Bitmap bmp, IntPtr hwndTarget, Rectangle rectTarget)
{
const int SRCCOPY = 0x00CC0020;
const int HALFTONE = 4;
IntPtr hdcTarget = GetDC(hwndTarget);
IntPtr hdcSource = CreateCompatibleDC(hdcTarget);
SelectObject(hdcSource, bmp.GetHbitmap());
SetStretchBltMode(hdcTarget, HALFTONE);
StretchBlt(hdcTarget, rectTarget.X, rectTarget.Y, rectTarget.Width, rectTarget.Height, hdcSource, 0, 0, bmp.Width, bmp.Height, SRCCOPY);
DeleteDC(hdcSource);
ReleaseDC(hwndTarget, hdcTarget);
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool StretchBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSrc, int xSrc, int ySrc, int wSrc, int hSrc, uint rop);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool SetStretchBltMode(IntPtr hdc, int mode);
Mit anpassen meine ich, dass eine Scrollbar erscheinen sollte, wenn die Grafik über die PictureBox hinausragt.
So erscheint eine Scrollbar, bei größeren Grafiken: pictureBox1.Image = bmp;
Die PictureBox liegt dabei auf einem Panel und und die Eigenschaften sind folgendermaßen gesetzt:
PictureBox Dock = None
PictureBox.SizeMode = AutoSize
Panel.AutoScroll = True
Die Frage ist, wie kann ich zur Laufzeit eine Grafik auswählen, welche um einen Faktor skaliert wird und entsprechend auf der PictureBox abgebildet wird, welche sich mit anpasst (Scrollbars), und das möglichst schnell?