no bmp displayed | Newbie | | Join Date: Nov 2008 Location: UK
Posts: 28
| |
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) - using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Drawing.Imaging;
-
-
namespace Test {
-
public partial class Form1 : Form {
-
public Form1() {
-
InitializeComponent();
-
}
-
-
Bitmap m_Bitmap = new Bitmap(2, 2);
-
Bitmap m_Undo = new Bitmap(2, 2);
-
private double Zoom = 1.0;
-
-
private void loadToolStripMenuItem_Click(object sender, EventArgs e) {
-
OpenFileDialog openFileDialog = new OpenFileDialog();
-
openFileDialog.InitialDirectory = "c:/Desktop/";
-
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
-
openFileDialog.FilterIndex = 1;
-
openFileDialog.RestoreDirectory = true;
-
-
if (DialogResult.OK == openFileDialog.ShowDialog()) {
-
m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
-
this.AutoScroll = true;
-
this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
-
this.Invalidate();
-
}
-
}
-
-
private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
-
SaveFileDialog saveFileDialog = new SaveFileDialog();
-
saveFileDialog.InitialDirectory = "c:/Desktop/";
-
saveFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
-
saveFileDialog.FilterIndex = 1;
-
saveFileDialog.RestoreDirectory = true;
-
-
if (DialogResult.OK == saveFileDialog.ShowDialog()) {
-
m_Bitmap.Save(saveFileDialog.FileName);
-
}
-
}
-
-
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
-
this.Close();
-
}
-
-
private void toolStripMenuItem2_Click(object sender, EventArgs e) {
-
Zoom = .25;
-
AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
-
Invalidate();
-
}
-
-
-
-
private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
-
Bitmap temp = (Bitmap)m_Bitmap.Clone();
-
m_Bitmap = (Bitmap)m_Undo.Clone();
-
m_Undo = (Bitmap)temp.Clone();
-
this.Invalidate();
-
}
-
-
private void invertToolStripMenuItem_Click(object sender, EventArgs e) {
-
m_Undo = (Bitmap)m_Bitmap.Clone();
-
if (Invert(m_Bitmap))
-
this.Invalidate();
-
}
-
-
private void greyscaleToolStripMenuItem_Click(object sender, EventArgs e) {
-
m_Undo = (Bitmap)m_Bitmap.Clone();
-
if (GrayScale(m_Bitmap))
-
this.Invalidate();
-
}
-
-
-
public static bool Invert(Bitmap b) {
-
// GDI+ still lies to us - the return format is BGR, NOT RGB.
-
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
-
-
int stride = bmData.Stride;
-
System.IntPtr Scan0 = bmData.Scan0;
-
-
unsafe {
-
byte* p = (byte*)(void*)Scan0;
-
-
int nOffset = stride - b.Width * 3;
-
int nWidth = b.Width * 3;
-
-
for (int y = 0; y < b.Height; ++y) {
-
for (int x = 0; x < nWidth; ++x) {
-
p[0] = (byte)(255 - p[0]);
-
++p;
-
}
-
p += nOffset;
-
}
-
}
-
-
b.UnlockBits(bmData);
-
-
return true;
-
}
-
-
public static bool GrayScale(Bitmap b) {
-
// GDI+ still lies to us - the return format is BGR, NOT RGB.
-
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
-
-
int stride = bmData.Stride;
-
System.IntPtr Scan0 = bmData.Scan0;
-
-
unsafe {
-
byte* p = (byte*)(void*)Scan0;
-
-
int nOffset = stride - b.Width * 3;
-
-
byte red, green, blue;
-
-
for (int y = 0; y < b.Height; ++y) {
-
for (int x = 0; x < b.Width; ++x) {
-
blue = p[0];
-
green = p[1];
-
red = p[2];
-
-
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
-
-
p += 3;
-
}
-
p += nOffset;
-
}
-
}
-
-
b.UnlockBits(bmData);
-
-
return true;
-
}
-
-
}
-
}
thnx
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | 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. |  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | 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
| | | 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
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: no bmp displayed Quote:
Originally Posted by mrcw 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 - private void loadToolStripMenuItem_Click(object sender, EventArgs e) {
-
OpenFileDialog openFileDialog = new OpenFileDialog();
-
openFileDialog.InitialDirectory = "c:/Desktop/";
-
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
-
openFileDialog.FilterIndex = 1;
-
openFileDialog.RestoreDirectory = true;
-
-
if (DialogResult.OK == openFileDialog.ShowDialog()) {
-
m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
-
this.AutoScroll = true;
-
this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
-
this.Invalidate();
-
}
-
}
Line 9 assigns to m_Bitmap but then m_Bitmap is never put into a PictureBox, or made to the be background of anything. - private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
-
Bitmap temp = (Bitmap)m_Bitmap.Clone();
-
m_Bitmap = (Bitmap)m_Undo.Clone();
-
m_Undo = (Bitmap)temp.Clone();
-
this.Invalidate();
-
}
-
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
| | | 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
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|