|
In my application I have a picturebox control which displays a bitmap I have created from a byte array. This works quite well most of the time, however if I resize the form the image will sometimes get corrupt. Sometimes it even draws other bitmaps that are being used by completely different forms.
This[^] is what the picturebox looks like when it's doing what it's supposed to.
Random corruption[^] Note that this isn't actually random at all. Whenever I get a pattern similar to this, it's the exact same pattern.
More corruption[^]
I haven't overridden the paint event for the picturebox or anything fancy like that. The only owner-drawn control in the form is the rainbow colour bar you can see on the right.
I've googled this to death and haven't been able to come up with anything, I'm really lost here. Help is well-appreciated.
|
|
|
|
|
How did you create the bitmap from the byte array?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
private Bitmap createBitmap(uint width, uint height, byte[] pixels)
{
if (width == 0)
{
throw new ArgumentException("Bitmap width must be greater than 0.");
}
if (height == 0)
{
throw new ArgumentException("Bitmap height must be greater than 0.");
}
UInt32 byteCount = width * height * 4;
System.IntPtr pPixels = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)(byteCount));
System.Runtime.InteropServices.Marshal.Copy(pixels, 0, pPixels, pixels.Length);
Bitmap bmp = new Bitmap((int)width, (int)height, (int)width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, pPixels);
System.Runtime.InteropServices.Marshal.FreeHGlobal(pPixels);
return bmp;
}
|
|
|
|
|
Hi,
the corruption you are seeing is typical when there is a mismatc in the image width, i.e. when scanlines are wider/narrower than the metadata indicates.
There are many ways to create a bitmap; any way that creates them from raw data requires the data to remain available as long as the image lives (i.e. isn't garbage collected), e.g.:
1. Image.FromFile() locks the file, as it may need to read the file again;
2. Image.FromStream() reqeusts the stream remains available and intact (although it often doesn't matter);
3. new Bitmap(..., rawDataPointer,...) requires the raw data to be available permanently.
1. enforces the file presence by locking the file (of course you could still get disconnected from the drive); 2 and 3 can't enforce anything, and are the likely cause for your image corruption.
BTW: all the above is in the fine manual, so RTFM.
|
|
|
|
|
Thanks for the info, that will be very helpful..
I guess C# has spoiled me... it has much better intellisense support than C++ - as a result I haven't found myself needing to read any of the documentation on msdn
|
|
|
|
|
You're welcome.
Whatever intellistuff they add, as long as MSDN holds more, one should read it. The remarks section is quite valuable most of the time.
|
|
|
|
|
Hi i am new in c#.
I am developing an application which have a main form(frmstart) and second form (frmreader).
i am moving from frmstart to frmReader form by a button click.
it work fine.
before moving to frmReader i hide frmStart. now i am on frmReader and i want to return back to my previous frmStart form. which is hidden. please tell me how to unhide the form.
# code MovetoFrmReader
frmReader R1 = new frmReader();
this.Hide();
R1.Show();
#code Returmback to frmStart
this.Dispose();
???
here in place of ??? what i need to write.
Please Help me.
Thanks
Ravindra
|
|
|
|
|
Firstly, don't use Dispose, use Close instead - it will be disposed later when the CG gets round to it. Remember that your form may have results that the main form expects to be able to pick up.
Instead, either
1) Pass the frmStart instance to frmReader (not brilliant, but it will work) and use the Show method on it before you Close() frmReader. This may give you problems if you miss a way to close teh form that you are not handling.
or
2) Use R1.ShowDialog(); instead of R1.Show(); This will prevent frmStart from proceeding until frmReader is closed. It can then unhide itself. (Much preferable, as frmReader can be more generic and does not need to know about the existance of frmStart)
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
OriginalGriff wrote: it will be disposed later when the CG gets round to it.
Nah. I don't expect Christian flies around the world collecting our junk.
|
|
|
|
|
Good spot!
|
|
|
|
|
Luc Pattyn wrote: I don't expect Christian flies around the world collecting our junk.
He can't - all airlines suck today
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Handle the FormClosing event of R1 in frmstart and it can unhide itself.
using System;
using System.Windows.Forms;
public partial class FormStart : Form
{
public FormStart()
{
InitializeComponent();
Click += new EventHandler(FormStart_Click);
}
private void FormStart_Click(object sender, EventArgs e)
{
ShowFormReader();
}
private void ShowFormReader()
{
FormReader formReader = new FormReader();
formReader.FormClosing += new FormClosingEventHandler(formReader_FormClosing);
formReader.Show();
Hide();
}
private void formReader_FormClosing(object sender, FormClosingEventArgs e)
{
Show();
}
}
If it's not a close of R1 that triggers this then you can easily create a custom event and handle that.
|
|
|
|
|
WRT your code, I want to get data from FormReader to FormStart.
I uses following 2 approaches:
A)
FormReader formReader = new FormReader(this);
Now, making FormStart member as internal/public accessing them on FormReader.
B)On click(here its closing) event handler as you did
private void formReader_FormClosing(object sender, FormClosingEventArgs e)
{
}
But here i have to make FormReader Member internal/public
My question is,
Which approach is good?
As in first we are making parent form member public,
But,In 2nd we are child form member public.
Or is there any other way?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Also If i have a case like
interface IA
{
void M();
}
interface IB
{
void M();
}
class ABC : IA, IB
{
public void M()
{
MessageBox.Show("Hello");
}
}
ABC a = new ABC();
a.M();
Whose Interface method i am implementing?
Its running not giving error?
What is the use of this definition ?
And what should i do if i want to implement IA not IB or vice versa.
Or by default its calling/implementing IA M, as it's mentioned 1st as ABC:IA,IB
|
|
|
|
|
With regard to your first question - getting data from FormReader to FormStart :
Do NOT use method A. It works but it is a nasty hack that as things get more complex you will regret!
Method B does not require any change to the access modifier of FormReader . The sender parameter will (normally) be the FormReader instance. So, inside the handler add something like this:
FormReader formReader = sender as FormReader;
if(formReader != null)
{
}
There is another method if you want FormReader to inform FormStart that there is data it may be interested in - you can raise a custom event in FormReader and subscribe to that in FormStart . Have a look at this tip[^], this tip[^], then this article[^] if this would fit your case better.
|
|
|
|
|
Sorry, forgot your second question!
The answer is neither. An interface has no implementation so you never actually use any methods/properties/events they appear to have. You always use the implementation in the class that provides the implementation.
|
|
|
|
|
Dave's approach is fine.
Maybe a simpler approach is also acceptable: if the one form is hidden while the other is shown, the latter probably could be shown as a dialog, hence:
this.Hide();
otherForm.ShowDialog();
this.Show();
|
|
|
|
|
I finally made it working!!! I made a test here actually and i made it work. I can save and load from a text file...yeeey.
How can I made it more compact? I want to transform it into a stand alone Class that I can call it from anywhere in any situation. Any suggestions?
Thanks
Here is the code:
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 SaveLoadNamespace;
using WarningsNamespace;
using System.Text.RegularExpressions;
namespace test7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SaveLoadClass slc = new SaveLoadClass();
WarningsClass wc = new WarningsClass();
string tampon;
private void buttonSave_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
tampon = "checkBox = true" + "\r\n";
}
else
{
tampon = "checkBox = false" + "\r\n";
}
tampon += "numericUpDown = " + numericUpDown1.Value.ToString() + "\r\n"; ;
tampon += "comboBox = " + comboBox1.Text + "\r\n"; ;
slc.file_SaveFile(tampon); label2.Text = wc.w2;
}
string buffer, s = ""; int i, j = 0;
private void buttonLoad_Click(object sender, EventArgs e)
{
slc.file_LoadFile(); label1.Text = buffer = slc.textToLoadInto; label3.Text = wc.w5;
#region <_____ NumericUpDown _____>
if (buffer.Contains("numericUpDown"))
{
i = buffer.IndexOf("numericUpDown");
s = buffer.Remove(0, i);
if (s.Contains("\r\n"))
{
i = s.IndexOf("\r\n");
j = s.Length - i;
s = s.Remove(i, j);
}
if (s.Contains("="))
{
i = s.IndexOf("=") + 1;
s = s.Remove(0, i).Trim();
}
i = int.Parse(s);
numericUpDown1.Value = i;
}
#endregion >NumericUpDown-END<
#region <_____ checkBox _____>
if (buffer.Contains("checkBox"))
{
i = buffer.IndexOf("checkBox");
s = buffer.Remove(0, i);
if (s.Contains("\r\n"))
{
i = s.IndexOf("\r\n");
j = s.Length - i;
s = s.Remove(i, j);
}
if (s.Contains("="))
{
i = s.IndexOf("=") + 1;
s = s.Remove(0, i).Trim();
}
if (s.Contains("true"))
{
checkBox1.Checked = true;
}
if (s.Contains("false"))
{
checkBox1.Checked = false;
}
}
#endregion >checkBox-END<
#region <_____ comboBox _____>
if (buffer.Contains("comboBox"))
{
i = buffer.IndexOf("comboBox");
s = buffer.Remove(0, i);
if (s.Contains("\r\n"))
{
i = s.IndexOf("\r\n");
j = s.Length - i;
s = s.Remove(i, j);
}
if (s.Contains("="))
{
i = s.IndexOf("=") + 1;
s = s.Remove(0, i).Trim();
}
comboBox1.Text = s;
}
#endregion >comboBox-END<
}
}
}
The output is like this:
checkBox = true
numericUpDown = 24
comboBox = red
|
|
|
|
|
Hi,
you could build a method to which you can pass a form. To get the controls at the passed form use the Controls-Property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx[^]
Check the type of each control to get the name like 'checkBox' for your string.
Btw: Instead of using the type-name of each control I would suggest using the ID, so that you can store more than one checkbox for example.
Hope this helps you a bit.
Regards
Sebastian
|
|
|
|
|
Its a good suggestion and i understand it because i see it some time ago used by others ... But, its too advanced for me for the moment and I wish to make it in some limited way that i can understand what i write there... But,I will look into it and I will strive to understand it how its done and try to use it...but small chance for me to accomplish that kind of maneuverability.
Thanks
|
|
|
|
|
My idea is to make some overloads methods for: [int,string,bool](i suppose even for a object but is a to high standard for me). What do you think?
|
|
|
|
|
What possessed you to declare a local variable called tampon ?
/ravi
|
|
|
|
|
I was actually thinking on "TRAIN Buffer" ,but in my language "tampon" actually is the word for "TRAIN Buffer"or 1.damper,2. bumper,3. fender, 4. shock, 5. buffer. I was not thinking at all at other meanings other than what i described ... sorry if i mislead you. I will change his name soon.
|
|
|
|
|
That's OK - it just made me laugh.
/ravi
|
|
|
|
|
Congratulations on getting your code to work This approach isnt very dynamic tho and if you add or remove fields you would have to modify your save/load methods. I suggest that you read some articles about Serialization. Start with XML serialization, that way you can open the xml file in your browser and see everything. binary serialization isnt very fun to look at :].
A FOO walked into a BAR, and the horse said..
|
|
|
|
|
I think I made it how I like it the most and i can proudly name it that is all right ... 2 nights over this but I bring it down right where I want it to be. Phew.
Now I am happy with it.
I know i must learn and assimilate /the faster the better/ the arrays and lists that you all master them soooo well, and i am angry for that, but... i will.
About this matter, some good exercises that cant be done without them(arrays&lists&othersLikeThem) ,and who can be done without make me thinking about what I know already but direct me to learn them well? Thanks,and I appreciate.
So, here is my Class:
using System.Windows.Forms;
using SaveLoadNamespace;
namespace ExtractionsNamespace
{
public class ExtractionsClass
{
SaveLoadClass slc = new SaveLoadClass();
public string tampon;
#region <_____ AddControl _____>
#endregion >AddControl-END<
public void AddControl(Control c, bool checkToAdd)
{
if (checkToAdd)
tampon += c.Name + " = true" + "\r\n";
else tampon += c.Name + " = false" + "\r\n";
}
public void AddControl(Control c, string textToAdd)
{
tampon += c.Name + " = " + textToAdd + " \r\n";
}
public void AddControl(Control c, int valueToAdd)
{
tampon += c.Name + " = " + valueToAdd + " \r\n";
}
public void EraseAll()
{
tampon = "";
}
public string buffer; string s = ""; int i, j = 0; bool tf;
private void Extracttor(string _TextToExtract_)
{
i = buffer.IndexOf(_TextToExtract_);
s = buffer.Remove(0, i);
if (s.Contains("\r\n"))
{
i = s.IndexOf("\r\n");
j = s.Length - i;
s = s.Remove(i, j);
}
if (s.Contains("="))
{
i = s.IndexOf("=") + 1;
s = s.Remove(0, i).Trim();
}
}
public int Extract_int(string _TextToSearchFor_)
{
if (buffer.Contains(_TextToSearchFor_))
{
Extracttor(_TextToSearchFor_);
i = int.Parse(s);
}
return i;
}
public bool Extract_bool(string _TextToSearchFor_)
{
if (buffer.Contains(_TextToSearchFor_))
{
Extracttor(_TextToSearchFor_);
}
if (s.Contains("true"))
{
tf = true;
}
if (s.Contains("false"))
{
tf = false;
}
return tf;
}
public string Extract_string(string _TextToSearchFor_)
{
if (buffer.Contains(_TextToSearchFor_))
{
Extracttor(_TextToSearchFor_);
return s;
}
return "string_?";
}
public void file_SaveFileX()
{
slc.file_SaveFile(tampon);
}
public string file_LoadFileX()
{
slc.file_LoadFile();
return buffer = slc.textToLoadInto;
}
}
}
And here is the implementation on form:
using System;
using System.Windows.Forms;
using WarningsNamespace;
using ExtractionsNamespace;
namespace test7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WarningsClass wc = new WarningsClass();
ExtractionsClass ex = new ExtractionsClass();
private void buttonSave_Click(object sender, EventArgs e)
{
ex.EraseAll();
ex.AddControl(checkBox1, checkBox1.Checked);
ex.AddControl(checkBox2, checkBox2.Checked);
ex.AddControl(checkBox3, checkBox3.Checked);
ex.AddControl(numericUpDown1, numericUpDown1.Value.ToString());
ex.AddControl(numericUpDown2, numericUpDown2.Value.ToString());
ex.AddControl(comboBox1, comboBox1.Text);
ex.AddControl(progressBar1, progressBar1.Value);
ex.AddControl(radioButton1, radioButton1.Checked);
ex.AddControl(radioButton2, radioButton2.Checked);
ex.AddControl(radioButton3, radioButton3.Checked);
ex.file_SaveFileX(); label2.Text = wc.w2;
}
private void buttonLoad_Click(object sender, EventArgs e)
{
label1.Text = ex.file_LoadFileX(); label3.Text = wc.w5;
checkBox1.Checked = ex.Extract_bool("checkBox1");
checkBox2.Checked = ex.Extract_bool("checkBox2");
checkBox3.Checked = ex.Extract_bool("checkBox3");
numericUpDown1.Value = ex.Extract_int("numericUpDown1");
numericUpDown2.Value = ex.Extract_int("numericUpDown2");
comboBox1.Text = ex.Extract_string("comboBox1");
progressBar1.Value = ex.Extract_int("progressBar1");
radioButton1.Checked = ex.Extract_bool("radioButton1");
radioButton2.Checked = ex.Extract_bool("radioButton2");
radioButton3.Checked = ex.Extract_bool("radioButton3");
}
#region <_____ mouseProgressBar _____>
int somemousecoordonates;
private void progressBar1_MouseMove(object sender, MouseEventArgs e)
{
somemousecoordonates = e.X;
}
private void progressBar1_Click(object sender, EventArgs e)
{
progressBar1.Value = somemousecoordonates;
label2.Text = somemousecoordonates.ToString();
}
#endregion >mouseProgressBar-END<
BTW, this is what i call a perfect example of [Code Library]. Something like this somewhere, cant be found? Of course for other subjects and matters(not just for savings or loadings).
And the Output is looking like this somewhere in a text file:
checkBox1 = true
checkBox2 = false
checkBox3 = true
numericUpDown1 = 14
numericUpDown2 = 56
comboBox1 = red
progressBar1 = 5
radioButton1 = false
radioButton2 = true
radioButton3 = false
Thanks for all the support.
|
|
|
|
|