Connecting Tech Pros Worldwide Forums | Help | Site Map

no bmp displayed

Newbie
 
Join Date: Nov 2008
Location: UK
Posts: 28
#1: Oct 14 '09
I am trying to adapt this code. It first shows a menu and alows you to select a .bmp, and the changes it when you select the menu entry. The problem is it doesn't display the bitmap in the form. I know I could display the bmp in a picturebox, but then the "invert" filter doesnt work.

Does anybody know a way to show the bmp and invert the picture. (Invert works)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Imaging;
  10.  
  11. namespace Test {
  12.     public partial class Form1 : Form {
  13.         public Form1() {
  14.             InitializeComponent();
  15.         }
  16.  
  17.        Bitmap m_Bitmap = new Bitmap(2, 2);
  18.        Bitmap m_Undo = new Bitmap(2, 2);
  19.        private double Zoom = 1.0;
  20.  
  21.         private void loadToolStripMenuItem_Click(object sender, EventArgs e) {
  22.             OpenFileDialog openFileDialog = new OpenFileDialog();
  23.             openFileDialog.InitialDirectory = "c:/Desktop/";
  24.             openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
  25.             openFileDialog.FilterIndex = 1;
  26.             openFileDialog.RestoreDirectory = true;
  27.  
  28.             if (DialogResult.OK == openFileDialog.ShowDialog()) {
  29.                 m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
  30.                this.AutoScroll = true;
  31.                 this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
  32.                 this.Invalidate();
  33.             }
  34.         }
  35.  
  36.         private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
  37.             SaveFileDialog saveFileDialog = new SaveFileDialog();
  38.              saveFileDialog.InitialDirectory = "c:/Desktop/";
  39.             saveFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
  40.             saveFileDialog.FilterIndex = 1;
  41.             saveFileDialog.RestoreDirectory = true;
  42.  
  43.             if (DialogResult.OK == saveFileDialog.ShowDialog()) {
  44.                 m_Bitmap.Save(saveFileDialog.FileName);
  45.             }
  46.         }
  47.  
  48.         private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
  49.             this.Close();
  50.         }
  51.  
  52.         private void toolStripMenuItem2_Click(object sender, EventArgs e) {
  53.             Zoom = .25;
  54.             AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
  55.             Invalidate();
  56.         }
  57.  
  58.  
  59.  
  60.         private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
  61.             Bitmap temp = (Bitmap)m_Bitmap.Clone();
  62.             m_Bitmap = (Bitmap)m_Undo.Clone();
  63.             m_Undo = (Bitmap)temp.Clone();
  64.             this.Invalidate();
  65.         }
  66.  
  67.         private void invertToolStripMenuItem_Click(object sender, EventArgs e) {
  68.             m_Undo = (Bitmap)m_Bitmap.Clone();
  69.             if (Invert(m_Bitmap))
  70.                 this.Invalidate();
  71.         }
  72.  
  73.         private void greyscaleToolStripMenuItem_Click(object sender, EventArgs e) {
  74.             m_Undo = (Bitmap)m_Bitmap.Clone();
  75.             if (GrayScale(m_Bitmap))
  76.                 this.Invalidate();
  77.         }
  78.  
  79.  
  80.         public static bool Invert(Bitmap b) {
  81.             // GDI+ still lies to us - the return format is BGR, NOT RGB.
  82.             BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  83.  
  84.             int stride = bmData.Stride;
  85.             System.IntPtr Scan0 = bmData.Scan0;
  86.  
  87.             unsafe {
  88.                 byte* p = (byte*)(void*)Scan0;
  89.  
  90.                 int nOffset = stride - b.Width * 3;
  91.                 int nWidth = b.Width * 3;
  92.  
  93.                 for (int y = 0; y < b.Height; ++y) {
  94.                     for (int x = 0; x < nWidth; ++x) {
  95.                         p[0] = (byte)(255 - p[0]);
  96.                         ++p;
  97.                     }
  98.                     p += nOffset;
  99.                 }
  100.             }
  101.  
  102.             b.UnlockBits(bmData);
  103.  
  104.             return true;
  105.         }
  106.  
  107.         public static bool GrayScale(Bitmap b) {
  108.             // GDI+ still lies to us - the return format is BGR, NOT RGB.
  109.             BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  110.  
  111.             int stride = bmData.Stride;
  112.             System.IntPtr Scan0 = bmData.Scan0;
  113.  
  114.             unsafe {
  115.                 byte* p = (byte*)(void*)Scan0;
  116.  
  117.                 int nOffset = stride - b.Width * 3;
  118.  
  119.                 byte red, green, blue;
  120.  
  121.                 for (int y = 0; y < b.Height; ++y) {
  122.                     for (int x = 0; x < b.Width; ++x) {
  123.                         blue = p[0];
  124.                         green = p[1];
  125.                         red = p[2];
  126.  
  127.                         p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
  128.  
  129.                         p += 3;
  130.                     }
  131.                     p += nOffset;
  132.                 }
  133.             }
  134.  
  135.             b.UnlockBits(bmData);
  136.  
  137.             return true;
  138.         }
  139.  
  140.     }
  141. }
thnx

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Oct 14 '09

re: no bmp displayed


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#3: Oct 14 '09

re: no bmp displayed


Quote:
Does anybody know a way to show the bmp and invert the picture. (Invert works)
If your invert works, then what is 'problem' part of the question and code?
Do you get an error message? Exception?

Have you put in breakpoints and walked through the code to see where it is behaving differently than you thought it would?
Newbie
 
Join Date: Nov 2008
Location: UK
Posts: 28
#4: Oct 14 '09

re: no bmp displayed


No error, it just doen't show the bmp. If the bmp showed I think the invert would be OK if it could see a bmp to invert
thnx
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#5: Oct 14 '09

re: no bmp displayed


Quote:

Originally Posted by mrcw View Post

No error, it just doen't show the bmp. If the bmp showed I think the invert would be OK if it could see a bmp to invert
thnx

Maybe I'm missing it, but I don't see anyplace you are actually using/display the bmp's you make

Expand|Select|Wrap|Line Numbers
  1. private void loadToolStripMenuItem_Click(object sender, EventArgs e) {
  2.             OpenFileDialog openFileDialog = new OpenFileDialog();
  3.             openFileDialog.InitialDirectory = "c:/Desktop/";
  4.             openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
  5.             openFileDialog.FilterIndex = 1;
  6.             openFileDialog.RestoreDirectory = true;
  7.  
  8.             if (DialogResult.OK == openFileDialog.ShowDialog()) {
  9.                 m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
  10.                this.AutoScroll = true;
  11.                 this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
  12.                 this.Invalidate();
  13.             }
  14.         }
Line 9 assigns to m_Bitmap but then m_Bitmap is never put into a PictureBox, or made to the be background of anything.

Expand|Select|Wrap|Line Numbers
  1.         private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
  2.             Bitmap temp = (Bitmap)m_Bitmap.Clone();
  3.             m_Bitmap = (Bitmap)m_Undo.Clone();
  4.             m_Undo = (Bitmap)temp.Clone();
  5.             this.Invalidate();
  6.         }
  7.  
You swap the bitmaps as a form of undo, but then the m_Bitmap doesn't go anywhere before you invalidate the form.

Where are you displaying the graphic?
Newbie
 
Join Date: Nov 2008
Location: UK
Posts: 28
#6: Oct 15 '09

re: no bmp displayed


sorted it. It works now. Thanks for your help and advice.
I added this code

protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;

g.DrawImage(m_Bitmap, new Rectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, (int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)));
}
mrcw
Reply