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
5 2461 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.
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?
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
@mrcw
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |