1Danke
ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
660
660
EMPFEHLEN
-
02.02.11 16:10 #1
- Registriert seit
- Jan 2011
- Ort
- Köln (NRW)
- Beiträge
- 16
edit: sorry gehört hier nicht hin sondern in .NET Grafik & Sound
Ist es möglich ein Bild in einer Konsolenanwendung anzeigen zu lassen?
Hab zwar hier m Forum was gefunden, aber leider nur unter c/c++:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <windows.h> #include <stdio.h> //#pragma comment(lib,"gdi32.lib") int LoadBmp2Console(char *szBitmap, int PosX, int PosY) { HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,szBitmap, IMAGE_BITMAP,0,0,LR_LOADFROMFILE); if (!hBitmap) return 1; BITMAP bmp; GetObject(hBitmap,sizeof(bmp),&bmp); HWND hwnd = FindWindow("ConsoleWindowClass",NULL); if (!hwnd) return 2; HDC hDC = GetDC(hwnd); if (!hDC) return 3; HDC hBitmapDC = CreateCompatibleDC(hDC); if (!hBitmapDC) return 4; SelectObject(hBitmapDC,hBitmap); BitBlt(hDC,PosX,PosY,bmp.bmHeight,bmp.bmWidth,hBitmapDC,0,0,SRCCOPY); DeleteObject(hBitmap); ReleaseDC(hwnd,hBitmapDC); ReleaseDC(hwnd,hDC); return 0; } int main() { int Status = LoadBmp2Console("bitmap.bmp",10,10); if(Status!=0)printf("Fehler: %i",Status); getchar(); return 0; }
Dachte eventuell an sowas hier:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
String source = @"c:\max.bmp"; Bitmap bild = new Bitmap(source); Int32 x, y, width, height; x = Console.WindowLeft; y = Console.WindowTop; width = Console.WindowWidth; height = Console.WindowHeight; using (Graphics objekt = Graphics.FromImage(bild)) { objekt.DrawImage(bild, new Rectangle(x, y, width, height)); } Console.ReadKey(); Console.Clear();Geändert von pipistrello (02.02.11 um 18:22 Uhr)
-
02.02.11 18:55 #2
- Registriert seit
- Oct 2007
- Beiträge
- 325
Du kannst einfach die Aufrufe, die in deinem C++ Code gemacht wurden, 1:1 zu C# übertragen. Du musst sie nur vorher per DllImport deklarieren:
Code csharp:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Diagnostics; namespace ShowImageInConsole { class Program { public const int LR_LOADFROMFILE = 0x0010; /// <summary> /// Loads a bitmap. /// </summary> public const int IMAGE_BITMAP = 0; /// <summary> /// Loads an icon. /// </summary> public const int IMAGE_ICON = 1; /// <summary> /// Loads a cursor. /// </summary> public const int IMAGE_CURSOR = 2; /// <summary> /// Loads an enhanced metafile. /// </summary> public const int IMAGE_ENHMETAFILE = 3; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad); [DllImport("gdi32.dll")] static extern int GetObject(IntPtr hgdiobj, int cbBuffer, IntPtr lpvObject); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetDC(IntPtr hWnd); [DllImport("gdi32.dll", SetLastError = true)] static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)] static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [DllImport("gdi32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); [DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr hObject); [DllImport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC); static void Main(string[] args) { int status = LoadBmp2Console(@"X:\bmp.bmp", 0, 0); if (status != 0) Console.WriteLine("Fehler: " + status); Console.ReadKey(); } private static int LoadBmp2Console(string path, int posX, int posY) { IntPtr hBitmap = LoadImage(IntPtr.Zero, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (hBitmap == IntPtr.Zero) return 1; IntPtr bmp = IntPtr.Zero; GetObject(hBitmap, IntPtr.Size, bmp); IntPtr hwnd = Process.GetCurrentProcess().MainWindowHandle; if (hwnd == IntPtr.Zero) return 2; IntPtr hDC = GetDC(hwnd); if (hDC == IntPtr.Zero) return 3; IntPtr hBitmapDC = CreateCompatibleDC(hDC); if (hBitmapDC == IntPtr.Zero) return 4; SelectObject(hBitmapDC, hBitmap); System.Drawing.Image image = System.Drawing.Image.FromHbitmap(hBitmap); int bmpHeight = image.Height; int bmpWidth = image.Width; image.Dispose(); if (!BitBlt(hDC, posX, posY, bmpWidth, bmpHeight, hBitmapDC, 0, 0, TernaryRasterOperations.SRCCOPY)) return 5; DeleteObject(hBitmap); ReleaseDC(hwnd, hBitmapDC); ReleaseDC(hwnd, hDC); return 0; } } public enum TernaryRasterOperations : uint { /// <summary>dest = source</summary> SRCCOPY = 0x00CC0020, /// <summary>dest = source OR dest</summary> SRCPAINT = 0x00EE0086, /// <summary>dest = source AND dest</summary> SRCAND = 0x008800C6, /// <summary>dest = source XOR dest</summary> SRCINVERT = 0x00660046, /// <summary>dest = source AND (NOT dest)</summary> SRCERASE = 0x00440328, /// <summary>dest = (NOT source)</summary> NOTSRCCOPY = 0x00330008, /// <summary>dest = (NOT src) AND (NOT dest)</summary> NOTSRCERASE = 0x001100A6, /// <summary>dest = (source AND pattern)</summary> MERGECOPY = 0x00C000CA, /// <summary>dest = (NOT source) OR dest</summary> MERGEPAINT = 0x00BB0226, /// <summary>dest = pattern</summary> PATCOPY = 0x00F00021, /// <summary>dest = DPSnoo</summary> PATPAINT = 0x00FB0A09, /// <summary>dest = pattern XOR dest</summary> PATINVERT = 0x005A0049, /// <summary>dest = (NOT dest)</summary> DSTINVERT = 0x00550009, /// <summary>dest = BLACK</summary> BLACKNESS = 0x00000042, /// <summary>dest = WHITE</summary> WHITENESS = 0x00FF0062, /// <summary> /// Capture window as seen on screen. This includes layered windows /// such as WPF windows with AllowsTransparency="true" /// </summary> CAPTUREBLT = 0x40000000 } }
Was bei der Arbeit mit windowseigenen Funktionen immer hilft ist die MSDN und www.pinvoke.net.Geändert von Masterclavat (02.02.11 um 21:59 Uhr) Grund: Kleine Verbesserungen am Code
-
02.02.11 23:39 #3
- Registriert seit
- Jan 2011
- Ort
- Köln (NRW)
- Beiträge
- 16
super vielen dank das ist doch was !
Werd die Tage mal versuchen das umzusetzen.
Ähnliche Themen
-
Effekte für Konsolenanwendung
Von dead_warrior1 im Forum C/C++Antworten: 7Letzter Beitrag: 09.01.10, 13:41 -
[c++] Konsolenanwendung CPU
Von XxbambamxX im Forum C/C++Antworten: 5Letzter Beitrag: 13.11.09, 08:00 -
Konsolenanwendung starten
Von Raabun im Forum .NET CaféAntworten: 5Letzter Beitrag: 27.03.09, 12:27 -
Konsolenanwendung und Farbe
Von foxxx im Forum C/C++Antworten: 12Letzter Beitrag: 25.04.05, 11:11 -
mit Konsolenanwendung kommunizieren
Von totherock im Forum .NET ArchivAntworten: 0Letzter Beitrag: 28.05.04, 10:46





Zitieren
Login





