|
Hey,
Ive been writing a simple image viewer program in C# and wanted to add the option to save a loaded image to another format. In the tiff format though its been a pain, i want to read in the tiff image's format, and give the option of changing that format. So far i do not know how to read an images properties, can anyone help?
Heres the proto source code ive done (bulit up mostly from CodeProject articles )
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ImageViewer
{
public class MyForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
protected MenuItem _itemNativeSize;
protected MenuItem _itemWindowSize;
protected int _filterIndex = -1;
protected Bitmap _myBitmap;
protected bool _nativeSize = true;
public MyForm()
{
InitializeComponent();
// set the initial text and size of the window
this.Text = "Image Viewer 0.0";
this.ClientSize = new Size(640, 480);
....
}
private void OnOpenImage(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(_filterIndex != -1)
ofd.FilterIndex = _filterIndex;
if(ofd.ShowDialog() == DialogResult.OK)
{
String filename = ofd.FileName;
if(filename.Length != 0)
{
_filterIndex = ofd.FilterIndex;
try
{
_myBitmap = new Bitmap(filename);
this.Text = "Image Viewer - " + filename;
this.AutoScroll = true;
this.AutoScrollMinSize = _myBitmap.Size;
this.Invalidate();
}
catch
{
MessageBox.Show(String.Format ("{0} is not " +
"a valid image file", filename), "Error");
}
}
}
}
private void OnSaveImage(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save Image As";
if(sfd.ShowDialog() == DialogResult.OK)
{
sfd.AddExtension = true;
String filename = sfd.FileName;
if(filename.EndsWith(".png"))
_myBitmap.Save(filename, ImageFormat.Png);
else if(filename.EndsWith(".tiff") || filename.EndsWith(".tif"))
_myBitmap.Save(filename, ImageFormat.Tiff);
else if(filename.EndsWith(".bmp"))
_myBitmap.Save(filename, ImageFormat.Tiff);
else if(filename.EndsWith(".jpeg") || filename.EndsWith(".jpg"))
_myBitmap.Save(filename, ImageFormat.Jpeg);
else if(filename.EndsWith(".gif"))
_myBitmap.Save(filename, ImageFormat.Gif);
else
MessageBox.Show("Unknown file type");
}
}
private void OnExit(object sender, EventArgs e)
{
this.Close();
}
protected override void OnPaint(PaintEventArgs e)
{
if(_myBitmap != null)
{
Graphics g = e.Graphics;
if(_nativeSize)
{
g.DrawImage(_myBitmap,
this.AutoScrollPosition.X,
this.AutoScrollPosition.Y,
_myBitmap.Width, _myBitmap.Height);
}
else
{
g.DrawImage(_myBitmap, this.ClientRectangle);
}
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
}
|
|
|
|
|
Hey All,
I have been printing to a Zebra barcode printer previously in vb6. I was able to print just a test line of text by using something similar to the following,
printer.print "This is a test"
I am trying the same little test using cSharp with this statement,
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, 0, 0);
This being an event that is called when my printPage_PrintDoc event is being fired. The printer seems to recognize that it is receiving text, and then just prints out a blank label. I even checked the ribben to ensure that its just not printing off of the label somewhere, as if I had bad x,y coordinates. Any suggestions would be greatly appreciated.
Thanks,
Ryan
|
|
|
|
|
Curious how a person would go about adding Visual Inheritance _after_ you have already created the forms. I am currently working on an MDI application and I decided to use VI on the majority of those forms.
Is there a way to add the inheritance after the fact ?
Rocky Moore <><
|
|
|
|
|
WinForms inherit from From as shown in top of your class' source code...
public class frmAbout : System.Windows.Forms.Form
Just make yourself another form (your new base form, somewhere down the line inheriting from System.Windows.Forms.Form) and replace the inheritance in your existing class.
It should look something like this:
public class MyNewBaseForm : System.Windows.Forms.Form<br />
{<br />
}
and
public class frmAbout : MyNewBaseForm
Should come up in the visual designer too...
|
|
|
|
|
Arjan Einbu wrote:
Should come up in the visual designer too...
Didn't come up nor at runtime. That is why I asked, trying to figure out what is missing. I even tried closing out all designers and closing VS.NET then reloading. Still no go.
Rocky Moore <><
|
|
|
|
|
Arjan Einbu wrote:
Should come up in the visual designer too...
For some reason, when I copied the project to another directory it lost the background image settings and the buttons on the form to be inherited happend to be off the view of the form. When I would run the application it did not show the background nor the buttons (since they were clipped). The designer still showed everything proper for the base window until today when I booted the system and went into the designer.
Anyway, now works as I thought it would have the first time.
Rocky Moore <><
|
|
|
|
|
How to Open the default browser from a listview from the doubleclicked column.
The Problem is i can create a new Process with IE but is there any other solution build in .net namespaces ?
Martin Lierschof
Junior Programming Assistant
World-Direct.at eBussines Solutions GmbH
mail²: martin.lierschof@world-direct.at
|
|
|
|
|
System.Diagnostics.Process.Start("http://www.codeproject.com");
It still starts a new process...
|
|
|
|
|
hmm thanks i found it allready, but i get this errormessage: "The requested section was not present in the activation context" on this url: "ftp://ftp.pku.edu.cn/pub/faq/rec.answers/mensa/";
Martin Lierschof
Junior Programming Assistant
World-Direct.at eBussines Solutions GmbH
mail²: martin.lierschof@world-direct.at
|
|
|
|
|
I have 2 forms. Once the first loads, the second is loaded, but hidden. I am trying to pass a string from Form1 to Form2, but have no idea how this is done. I cannot change the constructor in Form2 to have this string, as the string would not have been decided yet. I hope you understand what I mean. Does anyone have any ideas? Is there anything like a session variable?
|
|
|
|
|
Well, if you are display the second form from the first one then you just add a property to the second form.
You can also use a static global (eek.. Yep Global) class with a static property for the data you wish to pass. In most of my current apps, I have a static class called "ApplicationData". It holds any information that will be required by several classes (or forms). It is also where I hold in memory configuration data.
Rocky Moore <><
|
|
|
|
|
Thanks for the reply. This may sound really stupid, but I have just started coding in C#, and dont know how that is done. I thought if I set a global attribute in one class, that I can then call that one from another class. But I cant reference it directly, so was not sure of anyway to do it. But your way sounds good, however, I dont know how to do that. Have you seen any samples on this type of thing?
|
|
|
|
|
Well, in my applications I have a global static class such as:
public class ApplicationData<br />
{<br />
public static string aSharedString=string.Empty;<br />
public static string userName = string.Empty;<br />
public static int user_ID = -1;<br />
public static string currentDocumentName = "RalphyBoy.txt";<br />
}
It can be cleaned up with properties if you like or to add code for locking if thread saftey is a concern.
In my applications, this would have methods to read/write configuration data along with possibly to persist state of an application when required.
It is true that C# does not have any globals but it is also true that they do, it is that you wrap them up in a nice neat class and access the static instance of the class.
Rocky Moore <><
|
|
|
|
|
Using VC,the element events can be get by the following code,how can I get the html element events using c#?
STDMETHODIMP CEventSink::Invoke(DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS* pdispparams,
VARIANT* pvarResult,
EXCEPINFO* pexcepinfo,
UINT* puArgErr)
{
switch (dispidMember)
{
case DISPID_HTMLELEMENTEVENTS2_ONCLICK:
OnClick();
break;
default:
break;
}
return S_OK;
}
Thanks.;)
|
|
|
|
|
Take a look to this article
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=312
|
|
|
|
|
Thanks a lot.
|
|
|
|
|
Hi,
I know that if you make your applicaton using the Ms Excel 9.0 and automation it will work fine for 2000 and XP versions.
But If I make it using the references to XP, no way to use it in 200O ? isn´t it ?
|
|
|
|
|
No, I don't think so.
You would at the very least have to change the references to point at the mso9.dll etc... and it __might__ still work so long as you're not accesing features in code that don't exist.
|
|
|
|
|
I have a byte[] that contains what in C++ would be this struct:
struct<br />
{<br />
unsigned type : 5;<br />
unsigned level : 2;<br />
unsigned bouncing : 1;<br />
unsigned emp : 1;<br />
unsigned isBomb : 1;<br />
unsigned shrapCount : 5;<br />
unsigned fireType : 1;<br />
};
Is there a way to make the byte[] into a class in C# without doing each member seperately with bitwise AND & shifting?
|
|
|
|
|
You might find the BitVector32 class useful to base your code off of.
You would create references to your types with the following code:
BitVector32 bv = new BitVector32(0);<br />
BitVector32.Section type = BitVector32.CreateSection(31);<br />
BitVector32.Section level = BitVector32.CreateSection(3,type);<br />
BitVector32.Section bouncing = BitVector32.CreateSection(1, level);<br />
BitVector32.Section emp = BitVector32.CreateSection(1, bouncing);<br />
BitVector32.Section isBomb = BitVector32.CreateSection(1, emp);<br />
BitVector32.Section shrapCount = BitVector32.CreateSection(31, isBomb);<br />
BitVector32.Section fireType = BitVector32.CreateSection(1, shrapCount);
The values are accessed as follows:
bv[type] = typeValue;<br />
bv[bouncing] = 1;
Access to your values with no &, |, or <<
|
|
|
|
|
|
for my current c# project i need a ftp connection test.
Given: Username, Password, port, hostname or ip
i tried it with System.Net.Sockets.Socket but it seems to be to slow to test more than 10 ftps in a row, cause of connection delay. the returnvalue just have to be boolean for Online/Offline
Somebody know a fast method to test this?
thanks / regrards
Martin Lierschof
Junior Programming Assistant
World-Direct.at eBussines Solutions GmbH
mail²: martin.lierschof@world-direct.at
|
|
|
|
|
I have an IIS-managed server object, exposed to clients using a service/wellknown web.config entry. But I don't want clients to have to know the type of the object (currently defined in a shared library) -- rather I want them to just query for the interfaces they require. This would theoretically allow me to change the underlying type on the server without code changes on the client.
To achieve this I wanted to expose the type as System.Object, but does not seem to be allowed by the remoting system. Have I made a design mistake? If so, what should I be doing?
Jade Burton
Programmer
|
|
|
|
|
You'll have to expose the object as atleast its interface.
<br />
public IMyInterface GetIt()<br />
{<br />
return (IMyInterface)myObj;<br />
}
where myObj is an object of any class that implements the IMyInterface interface
|
|
|
|
|
You might be able to use internal on all other classes except your facades/interfaces. That should 'hide' those classes from all outside calls.
|
|
|
|