Bitmap in konsolenanwendung c# ?

pipistrello

Mitglied
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:
 #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:
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();
 
Zuletzt bearbeitet:
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:

C#:
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.
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück