Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

Why is image is upside down?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
assaf - 20 Sep 2004 13:33 GMT
hi all

i included the sources of my form.
when run, u can open a bmp file.
once opened, and resized,
u will see that the image is upside down.

can someone please tell me why this is happening?

tnx
assaf

the code:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
 private System.Windows.Forms.OpenFileDialog openFileDialog1;
 private System.Windows.Forms.MainMenu mainMenu1;
 private System.Windows.Forms.MenuItem menuItem1;
 private System.Windows.Forms.MenuItem menuItemOpen;
 /// <summary>
 /// Required designer variable.
 /// </summary>
 private System.ComponentModel.Container components = null;

 public Form1()
 {
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  //
  // TODO: Add any constructor code after InitializeComponent call
  //
 }

 /// <summary>
 /// Clean up any resources being used.
 /// </summary>
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
   if (components != null)
   {
    components.Dispose();
   }
  }
  base.Dispose( disposing );
 }

 #region Windows Form Designer generated code
 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InitializeComponent()
 {
  this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  this.mainMenu1 = new System.Windows.Forms.MainMenu();
  this.menuItem1 = new System.Windows.Forms.MenuItem();
  this.menuItemOpen = new System.Windows.Forms.MenuItem();
  //
  // openFileDialog1
  //
  this.openFileDialog1.FileOk += new
System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
  //
  // mainMenu1
  //
  this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                      this.menuItem1});
  //
  // menuItem1
  //
  this.menuItem1.Index = 0;
  this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                      this.menuItemOpen});
  this.menuItem1.Text = "&File";
  //
  // menuItemOpen
  //
  this.menuItemOpen.Index = 0;
  this.menuItemOpen.Text = "&Open...";
  this.menuItemOpen.Click += new
System.EventHandler(this.menuItemOpen_Click);
  //
  // Form1
  //
  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  this.AutoScroll = true;
  this.AutoScrollMinSize = new System.Drawing.Size(1124, 868);
  this.ClientSize = new System.Drawing.Size(276, 257);
  this.Menu = this.mainMenu1;
  this.Name = "Form1";
  this.Text = "Form1";
  this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

 }
 #endregion

 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 [STAThread]
 static void Main()
 {
  Application.Run(new Form1());
 }

 private void DrawDIB(IntPtr hdc, int XDest, int YDest, int DestWidth, int
DestHeight, int XSrc, int YSrc, int SrcWidth, int SrcHeight, uint rop)
 {
  Bitmap b = (Bitmap)this._Image;
  BitmapData bm = null;
  try
  {
   Rectangle r = new Rectangle(XSrc, YSrc, SrcWidth, SrcHeight);
   bm = b.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
   uint iStretchDIBits = Gdi32.StretchDIBits(hdc, XDest, YDest, DestWidth,
DestHeight, XSrc, YSrc, SrcWidth, SrcHeight, bm.Scan0, this._BITMAPINFO,
DIB_RGB_COLORS, rop);
   if(iStretchDIBits == Gdi32.GDI_ERROR)
   {
    throw new ApplicationException("StretchDIBits Failed");
   }
  }
  finally
  {
   if(bm != null)
   {
    b.UnlockBits(bm);
   }
  }
 }

 private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
 {
  if(this._Image != null)
  {
   IntPtr hdc = e.Graphics.GetHdc();

   Gdi32.SetWindowOrgEx(hdc, 0, 0, IntPtr.Zero);
   int horiz = - User32.GetScrollPos(this.Handle,
User32.ScrollPos.SB_HORZ);
   int vert =  - User32.GetScrollPos(this.Handle,
User32.ScrollPos.SB_VERT);
   Gdi32.SetViewportOrgEx(hdc, horiz, vert, IntPtr.Zero);

   int iSaveDC = Gdi32.SaveDC(hdc);
   int w = this._Image.Width;
   int h = this._Image.Height;
   this.DrawDIB(hdc, GAP, GAP, w, h, 0, 0, w, h, SRCCOPY);

   bool bRestoreDC = Gdi32.RestoreDC(hdc, -1);
   if(bRestoreDC == false)
   {
    throw new ApplicationException("RestoreDC Failed");
   }
   e.Graphics.ReleaseHdc(hdc);
  }
 }

 private const int DIB_RGB_COLORS = 0; // color table in RGBs
 private const int SRCCOPY = 0x00CC0020; // dest = source
 private const int GAP = 16;
 private Image _Image;
 private Gdi32.BITMAPINFO _BITMAPINFO = new Gdi32.BITMAPINFO();

 private void openFileDialog1_FileOk(object sender,
System.ComponentModel.CancelEventArgs e)
 {
  this._BITMAPINFO.bmiColors.rgbBlue  = 200;
  this._BITMAPINFO.bmiColors.rgbGreen  = 208;
  this._BITMAPINFO.bmiColors.rgbRed  = 212;
  this._BITMAPINFO.bmiColors.rgbReserved = 200;

  this._BITMAPINFO.bmiHeader.biSize   = 40;
  this._BITMAPINFO.bmiHeader.biWidth   = 1024;
  this._BITMAPINFO.bmiHeader.biHeight   = 768;
  this._BITMAPINFO.bmiHeader.biPlanes   = 1;
  this._BITMAPINFO.bmiHeader.biBitCount  = 24;
  this._BITMAPINFO.bmiHeader.biCompression = 0;
  this._BITMAPINFO.bmiHeader.biSizeImage  = 2359296;
  this._BITMAPINFO.bmiHeader.biXPelsPerMeter = 0;
  this._BITMAPINFO.bmiHeader.biYPelsPerMeter = 0;
  this._BITMAPINFO.bmiHeader.biClrUsed  = 0;
  this._BITMAPINFO.bmiHeader.biClrImportant = 0;

  this._Image = Image.FromFile(this.openFileDialog1.FileName);
 }

 private void menuItemOpen_Click(object sender, System.EventArgs e)
 {
  this.openFileDialog1.ShowDialog(this);
 }

 private class User32
 {
  internal enum ScrollPos
  {
   SB_HORZ = 0,
   SB_VERT = 1
  }

  [DllImport("user32.dll")]
  internal static extern
   int GetScrollPos(
   IntPtr hWnd,
   ScrollPos nBar
   );
 }

 private class Gdi32
 {
  [DllImport("gdi32.dll")]
  internal static extern
   bool SetViewportOrgEx(
   IntPtr hdc,        // handle to device context
   int X,          // new x-coordinate of viewport origin
   int Y,          // new y-coordinate of viewport origin
   IntPtr lpPoint // original viewport origin
   );

  [DllImport("gdi32.dll")]
  internal static extern
   bool SetWindowOrgEx(
   IntPtr hdc,        // handle to device context
   int X,          // new x-coordinate of window origin
   int Y,          // new y-coordinate of window origin
   IntPtr lpPoint // original window origin
   );

  [StructLayout(LayoutKind.Sequential)]
  internal struct BITMAPINFOHEADER
  {
   public uint   biSize;
   public int    biWidth;
   public int    biHeight;
   public short  biPlanes;
   public short  biBitCount;
   public uint   biCompression;
   public uint   biSizeImage;
   public int    biXPelsPerMeter;
   public int    biYPelsPerMeter;
   public uint   biClrUsed;
   public uint   biClrImportant;
  }

  [StructLayout(LayoutKind.Sequential)]
  internal struct RGBQUAD
  {
   public byte    rgbBlue;
   public byte    rgbGreen;
   public byte    rgbRed;
   public byte    rgbReserved;
  }

  [StructLayout(LayoutKind.Sequential)]
  internal class BITMAPINFO
  {
   public BITMAPINFOHEADER bmiHeader;

   //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
   //public RGBQUAD [] bmiColors = new RGBQUAD[1];
   public RGBQUAD bmiColors;
  }

  [DllImport("gdi32.dll")]
  internal static extern
   uint StretchDIBits(
   IntPtr /*HDC*/ hdc,                     // handle to DC
   int XDest,                       // x-coord of destination upper-left
corner
   int YDest,                       // y-coord of destination upper-left
corner
   int nDestWidth,                  // width of destination rectangle
   int nDestHeight,                 // height of destination rectangle
   int XSrc,                        // x-coord of source upper-left corner
   int YSrc,                        // y-coord of source upper-left corner
   int nSrcWidth,                   // width of source rectangle
   int nSrcHeight,                  // height of source rectangle
   IntPtr /*CONST VOID **/lpBits,   // bitmap bits
   BITMAPINFO /*CONST BITMAPINFO **/lpBitsInfo,// bitmap data
   uint iUsage,                    // usage options
   uint dwRop                      // raster operation code
   );

  internal const uint GDI_ERROR = 0xFFFFFFFF;

  [DllImport("gdi32.dll")]
  internal static extern
   bool RestoreDC(
   IntPtr /*HDC*/ hdc,       // handle to DC
   int nSavedDC   // restore state
   );

  [DllImport("gdi32.dll")]
  internal static extern
   int SaveDC(
   /*HDC*/IntPtr hdc   // handle to DC
   );
 }
}
}
vipin - 20 Sep 2004 13:28 GMT
You reinvent the wheel. GDI+ provides easy to use functions like
DrawImage(...) and I see that you have used StretchDIBits(...) to do it the
hard way. I ran it here and see nothing on the form,leave inversion. Why do
you want to do a simple thing in a difficult way and that too from c#. What
is the advantage of doing this way?

> hi all
>
[quoted text clipped - 318 lines]
>  }
> }
assaf - 21 Sep 2004 21:28 GMT
dot net background image
scrolls at 100% cpu.

assaf

> You reinvent the wheel. GDI+ provides easy to use functions like
> DrawImage(...) and I see that you have used StretchDIBits(...) to do it the
[quoted text clipped - 329 lines]
> >  }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.