|
Hi,
A typical image may vary because i am using different size images. Typical size may be 1024 x 768 or 640 x 480.
if you dont mind could you give me some sample code regarding #2 please?
Thanks,
|
|
|
|
|
Untested (see also my next message):
public Bitmap ResizeImageKeepingAspectRatio(Bitmap bm1, int w2, int h2, Brush backColor) {
int w1=bm1.Width;
int h1=bm1.Height;
Bitmap bm2=new Bitmap(w2, h2);
Graphics g=Graphics.FromImage(bm2);
g.FillRectangle(backColor, 0, 0, w2, h2);
int x2=0;
int y2=0;
float scale=1;
if (w1*h2>w2*h1) {
y2=h2-h1*w2/w1);
scale=w2/(float)w1;
} else {
x2=w2-w1*h2/h1);
scale=h2/(float)h1;
}
g.ScaleTransform(scale, scale);
g.TranslateTransform(x2/2, y2/2);
g.DrawImage(bm1, 0, 0);
g.Dispose();
return bm2;
}
You need to test this; I suggest you give it a 100*100 and convert it to first a 40*50, and then a 50*40.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
modified on Friday, February 26, 2010 6:54 PM
|
|
|
|
|
Hi Luc,
Thanks for your help. I do have a small problem regarding images which have 640(width)and 480(height). The right hand side of the image appears to be cut off so i cant see whats on the right hand side of the image. I did pass in the paramters as suggested.
I tried using the Width and Height property of ImageList size(100,100) and passed it into your method to test the if it would work. Now images which are 1024 by 768 scale nicely. I then increased the size of the ImageList to (150,150) but that hasnt solved the issue.
if i set the ImageListSize to (40,50) or vice versa than the images look small.
if i pass in just the values 40, 50 or 50, 40 into your method the images look small and the corner of the images are cut off(the 640 by 480 image).
I tried to adjust your code so i could fix the problem but i havent yet been able to solve it.
Can you advise please?
Thanks,
|
|
|
|
|
forget the 40,50 and 50,40; that was for testing my code only, which I did, see my other post.
No need to adjust my code any further, it does what its name implies.
If you can't get your stuff right, explain in detail and provide code and symptoms.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi,
I am testing the following code. Its just the images which are 640 x 480 appear to be cut off on the right hand side. What i mean is say you have a picture(640 x 480) of a Table but on the right side of the table there is something there, say a bowl of fruit, if i use the method below it cuts it off so you dont see the remaining contents of the image.
I did set the boolean parameter to true but i am not sure why the problem is occuring with those image sizes? I assumed that it would keep the aspect ratio.
public Bitmap ResizeImageKeepingAspectRatio(Image bm1, int w2, int h2, Brush backColor, bool centered)
{
int w1=bm1.Width;
int h1=bm1.Height;
Bitmap bm2=new Bitmap(w2, h2);
Graphics g=Graphics.FromImage(bm2);
g.FillRectangle(backColor, 0, 0, w2, h2);
int x2=0;
int y2=0;
float scale=1;
if (w1*h2 > w2*h1) {
if (centered) y2=h2*w1/w2-h1;
scale=w2/(float)w1;
} else {
if (centered) x2=w2*h1/h2-w1;
scale=h2/(float)h1;
}
g.ScaleTransform(scale, scale);
g.TranslateTransform(x2/2, y2/2);
g.DrawImage(bm1, 0, 0);
g.Dispose();
return bm2;
}
Here is the code i am using in the button click
StaffImageList.ImageSize = new Size(120, 120);
this.StaffListView.View = View.LargeIcon;
StaffImageList.Images.Add(fPath, ResizeImageKeepingAspectRatio(img,StaffImageList.ImageSize.Width,StaffImageList.ImageSize.Height, brush, true));
Could you advise please?
Thanks
|
|
|
|
|
I checked again with your sizes (640*480 -> 120*120) and my resizing method works as expected. You can verify by saving the returned image to a file, then inspecting it with whatever image viewer you have. If anything goes wrong, it would be in the ListView. I'm not all that familiar with it, I can't advise any further.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi,
Can you confirm the following please?
So did you use an image of 640 x 480?
Regarding the ImageList size, did you pass the ImageSize.Width and ImageSize.Height into your modified method?
I will try this again.
Thanks
|
|
|
|
|
As I said before, I used a 640*480 image and asked for it to be rescaled to 120*120, no ListView involved. All is well.
You should learn to debug, look at the end result and stare at your code; instead check your inputs, observe intermediate results, and if needed split statements into a few lines so you can see more. What I typically do is add log statements abundantly, logging relevant information to the Console and/or to a text file.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi,
I have sorted the issue out. Thanks.
|
|
|
|
|
fixed minor mistakes; now tested:
public static Bitmap ResizeImageKeepingAspectRatio(Image bm1, int w2, int h2, Brush backColor, bool centered) {
int w1=bm1.Width;
int h1=bm1.Height;
Bitmap bm2=new Bitmap(w2, h2);
Graphics g=Graphics.FromImage(bm2);
g.FillRectangle(backColor, 0, 0, w2, h2);
int x2=0;
int y2=0;
float scale=1;
if (w1*h2>w2*h1) {
if (centered) y2=h2*w1/w2-h1;
scale=w2/(float)w1;
} else {
if (centered) x2=w2*h1/h2-w1;
scale=h2/(float)h1;
}
g.ScaleTransform(scale, scale);
g.TranslateTransform(x2/2, y2/2);
g.DrawImage(bm1, 0, 0);
g.Dispose();
return bm2;
}
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
hi!
i need to know how to use my c# windows form application to generate a cookie on a host computer that can be then read by a php script.
the cookie will have a unique name so it can be selected by the script and it should have a vector structure...(how can i explain: i don't know of c#,but in php, cookies can hold many parameters like cookie['username']="alex" or something like that...)
so how can i solve this?
thanks in advance!
|
|
|
|
|
Are you deweloping web browser?
Cookie are stored difrently betwen browsers.
PHP creates cookie, send it to local computer. And request a cookie if exsist.
|
|
|
|
|
hullo, my name is IRO, I would like to design a voice chatting system.
the environment i am in access wireless, so i want to use this as a chance to get the system working.
Can anyone help me out please.
|
|
|
|
|
Around here we call that a cell phone. CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
hello, do you have any progress? i also haeve interesting in that subject.
|
|
|
|
|
Hi
I want to create controls dynamically in the form
Initially i have textbox1 besides this button1 (text as Plus)
when i click on plus button it has to create textbox2,button2(plus) and also button3(minus) .
e.g: whenever i click on button3(minus) it shouls collapse(remove textbox2,button2,button3) and so on but move all the below controls to top position .............. in this manner i want to add and remove controls .
thanks in advance...
|
|
|
|
|
Sounds like homework. And it's not very difficult; look through the auto-generated code from another form for hints.
|
|
|
|
|
You can move controls around on the form dynamically by setting their Control.Left and their Control.Top properties. As for creating new ones, it's also pretty simple, just create them in code, add them to the parent form then set their top and left Properties
TextBox t = new TextBox();
form1.Controls.Add(t);
t.Top = 10;
t.Left = 10;
that should do it.
|
|
|
|
|
1. Create a user control that has Button and TextBox in it.
2. Add a FlowLayoutPanel to your form and set its FlowDirection as TopDown .
3. Handle the button click event of the button in the Form itself (through delegate). And then add and remove controls at your will. They will move up/down automatically.
|
|
|
|
|
Hello,
How can i convert txt file to excell file?
The data in the file is seperated be spaces or tabs.
I know i can do it using copy-->paste but i need to write a program for the conversion
|
|
|
|
|
You can read the file through StreamReader and then write to the excel file.
|
|
|
|
|
ok but how do i seperate the data seperated by spaces or tabs in the text file to different cells in the excell file?
|
|
|
|
|
If it is tab, text would automatically be divided into cells. If it is space, you can replace them with tabs.
|
|
|
|
|
how do i replace space with tab? how do i write in the code to insert tab in a line?
|
|
|
|
|