Tôi cần trợ giúp đặt hình ảnh trong suốt vào khay nhớ tạm. Tôi tiếp tục nhận được "xử lý không hợp lệ". Về cơ bản, tôi cần một "bộ mắt thứ hai" để xem xét mã sau đây. (Dự án làm việc hoàn chỉnh tại ftp://missico.net/ImageVisualizer.zip.)Cần trợ giúp Đặt hình ảnh có nền trong suốt thành Clipboard
Đây là hình ảnh Thư viện lớp Visualizer Debug, nhưng tôi đã thực hiện dự án đi kèm để chạy dưới dạng tệp thi hành để thử nghiệm. (Lưu ý rằng cửa sổ là cửa sổ hộp công cụ và hiển thị trên thanh tác vụ được đặt thành false). Tôi đã mệt mỏi vì phải thực hiện chụp màn hình trên cửa sổ hộp công cụ, mở ảnh chụp màn hình bằng trình chỉnh sửa hình ảnh và sau đó xóa nền được thêm vào vì nó là một ảnh chụp màn hình. Vì vậy, tôi nghĩ rằng tôi sẽ nhanh chóng đưa hình ảnh trong suốt vào clipboard. Vâng, vấn đề là ... không hỗ trợ minh bạch cho Clipboard.SetImage. Google để giải cứu ... không hoàn toàn.
Đây là những gì tôi có cho đến nay. Tôi lấy từ một số nguồn. Xem mã cho tham chiếu chính. Vấn đề của tôi là "xử lý không hợp lệ" khi sử dụng CF_DIBV5. Tôi có cần sử dụng BITMAPV5HEADER và CreateDIBitmap không?
Bất kỳ trợ giúp nào từ bạn GDI/GDI + Wizards sẽ được đánh giá cao.
public static void SetClipboardData(Bitmap bitmap, IntPtr hDC) {
const uint SRCCOPY = 0x00CC0020;
const int CF_DIBV5 = 17;
const int CF_BITMAP = 2;
//'reference
//'http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/816a35f6-9530-442b-9647-e856602cc0e2
IntPtr memDC = CreateCompatibleDC(hDC);
IntPtr memBM = CreateCompatibleBitmap(hDC, bitmap.Width, bitmap.Height);
SelectObject(memDC, memBM);
using (Graphics g = Graphics.FromImage(bitmap)) {
IntPtr hBitmapDC = g.GetHdc();
IntPtr hBitmap = bitmap.GetHbitmap();
SelectObject(hBitmapDC, hBitmap);
BitBlt(memDC, 0, 0, bitmap.Width, bitmap.Height, hBitmapDC, 0, 0, SRCCOPY);
if (!OpenClipboard(IntPtr.Zero)) {
throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard", new Win32Exception());
}
if (!EmptyClipboard()) {
throw new System.Runtime.InteropServices.ExternalException("Unable to empty Clipboard", new Win32Exception());
}
//IntPtr hClipboard = SetClipboardData(CF_BITMAP, memBM); //works but image is not transparent
//all my attempts result in SetClipboardData returning hClipboard = IntPtr.Zero
IntPtr hClipboard = SetClipboardData(CF_DIBV5, memBM);
//because
if (hClipboard == IntPtr.Zero) {
// InnerException: System.ComponentModel.Win32Exception
// Message="The handle is invalid"
// ErrorCode=-2147467259
// NativeErrorCode=6
// InnerException:
throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard", new Win32Exception());
}
if (!CloseClipboard()) {
throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard", new Win32Exception());
}
g.ReleaseHdc(hBitmapDC);
}
}
private void __copyMenuItem_Click(object sender, EventArgs e) {
using (Graphics g = __pictureBox.CreateGraphics()) {
IntPtr hDC = g.GetHdc();
MemoryStream ms = new MemoryStream();
__pictureBox.Image.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
Image imag = Image.FromStream(ms);
// Derive BitMap object using Image instance, so that you can avoid the issue
//"a graphics object cannot be created from an image that has an indexed pixel format"
Bitmap img = new Bitmap(new Bitmap(imag));
SetClipboardData(img, hDC);
g.ReleaseHdc();
}
}
Bạn có thể làm rõ bước nào cung cấp cho bạn lỗi xử lý không hợp lệ không? –