473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

no bmp displayed

82
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
Oct 14 '09 #1
5 2461
tlhintoq
3,525 Expert 2GB
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.
Oct 14 '09 #2
tlhintoq
3,525 Expert 2GB
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?
Oct 14 '09 #3
mrcw
82
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
Oct 14 '09 #4
tlhintoq
3,525 Expert 2GB
@mrcw
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?
Oct 14 '09 #5
mrcw
82
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
Oct 15 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jefferis NoSpamme | last post by:
I have a query that is showing new items in a store based upon their inventory date. The problem with my Random order sort is that when the "next" link is pressed, the random function is called...
5
by: Bob | last post by:
I am displaying a form with a datagrid populated from a select of database records which now exceed 12,000 when I select them all. The form displays just fine with all 12,000+ entries but when I...
1
by: Bob | last post by:
I am displaying a form with a datagrid populated from a select of database records which now exceed 12,000 when I select them all. The form displays just fine with all 12,000+ entries but when I...
3
by: Carolyn Vo | last post by:
I have a datagrid in my web control class that I am trying to get the current rows displayed for. I have enabled paging on the datagrid so if the user is currently on page 3 of 8, and if I have...
2
by: MarkusPoehler | last post by:
After saving TEXT combined with HTML Tags into a HTM File by ASP.NET using filestream, the HTM Files is not displayed correctly in IE. Umlaute & e.g. € (Euro Sign) are not shown, I get some wrong...
2
by: vvenk | last post by:
Hello: I have a weblayout that uses frames. It has three frames, one on the top that displayes the header (header.aspx), one on the left that displays the system menu (menu.aspx) and one on the...
2
by: jburkle | last post by:
The following is the onclick method called when the "Renew" button is clicked by the user in my Windows application: ..... Private Sub cmdRenew_Click(ByVal eventSender As System.Object, ByVal...
2
by: Tim Reynolds | last post by:
Team, When my web service throws an exception and I am debugging on my PC - I get the exception back in my browser fine. My web.config shows <customErrors mode="Off"/> and this is working well. ...
0
by: Ide | last post by:
Hi I have a bug with some links on my website reposting the same aspx page with some filters: The filtered page is --correctly displayed using localhost with ie7 --correctly displayed using a...
1
by: Ide | last post by:
Hi I have a bug with some links on my website reposting the same aspx page with some filters: The filtered page is --correctly displayed using localhost with ie7 --correctly displayed using a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.