Here is a complete example, which is working perfectly on my PDA here. (you
will also need to add two picture boxes to display the before and after
results for the code below to work (pictureBox1 and pictureBox2))
private void Form1_Load(object sender, System.EventArgs e)
{
Bitmap img = new Bitmap("\\Naveen.jpg");
if (img != null)
{
//set the properties of a picture box for the original image
pictureBox2.Location = new Point(5,5);
pictureBox1.Image=img;
pictureBox1.Size=new Size(img.Width,img.Height);
//scale the bitmap then convert to gray scale
Bitmap scaledImg = ScaleBitmap(img, 0.5f, 0.5f);
convertToGrayScale(scaledImg);
//set the properties of a picture box for the scaled and gray scaled
image
pictureBox2.Location = new
Point(5,pictureBox1.Top+pictureBox1.Height);
pictureBox2.Size=new Size(scaledImg.Width,scaledImg.Height);
pictureBox2.Image=scaledImg;
}
}
public Bitmap ScaleBitmap(Bitmap src, float xscale, float yscale)
{
Bitmap res = new
Bitmap((int)(src.Width*xscale),(int)(src.Height*yscale));
using (Graphics g = Graphics.FromImage(res))
{
g.DrawImage(src, new Rectangle(0, 0, res.Width, res.Height), 0, 0,
src.Width, src.Height, GraphicsUnit.Pixel, new
System.Drawing.Imaging.ImageAttributes());
}
return res;
}
protected void convertToGrayScale(Bitmap scaledImg)
{
Color col;
int gray;
for (int l = 0; l<scaledImg.Width; l++)
for (int c = 0; c<scaledImg.Height; c++)
{
col = scaledImg.GetPixel(l,c);
gray = (int)(0.32f * col.R + 0.5f * col.G + 0.18f* col.B);
scaledImg.SetPixel(l,c,Color.FromArgb(gray, gray, gray));
}
}
Hope that helps,
Joseph
"Naveen koul" <(E-Mail Removed)> wrote in message
news:3479743A-6065-4D46-9436-(E-Mail Removed)...
> Sir,
>
> I tried the same thing , but when the cursor goes to the convert to grey
> scale function . it gives error moves to catch.were if i see img valur it
> shows up unfedined.
>
> i am giving the fragments of code
> ************************************************************
> First it loads the image from here this function
> private void radioButton1_CheckedChanged(object sender, System.EventArgs
> e)
> {
> try
> {
> img = new PMImage("\\Naveen.jpg");
>
> }
> catch {}
> if (img != null)
> {
> matchs = n.search(img);
>
> }
> }
> **********************************************************
> After this function it goes to to the scaling function
>
> public float Scale
> {
>
> set
> {
> scale = 1.0;
> Bitmap scaledImg = ScaleBitmap(img, 0.5f, 0.5f);
> convertToGrayScale();
> }
> }
> **********************************************************
> From this function it enters the ScaleBitmap function which was provided
> by
> you
>
> public Bitmap ScaleBitmap(Bitmap src, float xscale, float yscale)
> {
> Bitmap res = new Bitmap((int)(src.Width*xscale),(int)(src.Height*yscale));
> using (Graphics g = Graphics.FromImage(res))
> {
> g.DrawImage(src, new Rectangle(0, 0, res.Width, res.Height), 0, 0,
> src.Width, src.Height, GraphicsUnit.Pixel, new
> System.Drawing.Imaging.ImageAttributes());
> }
> return res;
> }
> **********************************************************
> from return res function goes back to Bitmap scaledImg, from there when
> the
> function goes to convertToGrayScale(), from there the dubigguer moves to
> catch m and when it checks the if condition it founds the img value
> =undefined, so come out it and just hang up.
>
>
> I hope i am more clear this time
>
> Regards
>
> naveen
>
>
> "Joseph Byrns" wrote:
>
>> Yes sorry, the line:
>>
>> Bitmap scaledImg = new
>> Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>>
>> in the example I sent should be:
>>
>> Bitmap scaledImg = new
>> Bitmap((int)(img.Width*scale),(int)(img.Height*scale));
>>
>>
>> The scaling is done below that line in the g.DrawImage(...)
>>
>>
>> "Naveen koul" <(E-Mail Removed)> wrote in message
>> news:946A2BE5-08FE-4EAD-9BD5-(E-Mail Removed)...
>> >
>> > Sir the function you have sent me is not working, this part in the
>> > function
>> > is not working as it shows error bitmap does not take three arguments,I
>> > have
>> > tried it before also.for this function i need alternative
>> > **********************************************************
>> >
>> > Bitmap scaledImg = new
>> > Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>> >
>> >
>> > ************************************************************
>> > Regards
>> >
>> > Naveen
>> >
>> > scale = value;
>> > Bitmap img = ....; your original bitmap here
>> > Bitmap scaledImg = new
>> > Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>> > "Joseph Byrns" wrote:
>> >
>> >> The function I sent you will scale the bitmap for you, it is the only
>> >> way
>> >> to
>> >> do it the compact framework (without writing your own scaling code
>> >> that
>> >> is).
>> >>
>> >> scale = value;
>> >> Bitmap img = ....; your original bitmap here
>> >> Bitmap scaledImg = new
>> >> Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>> >> Graphics g = Graphics.FrimImage(scaledImg);
>> >> g.DrawImage(img, new Rectangle(0,0,scaledImg.Width,scaledImg.Height),
>> >> new
>> >> Rectangle(0,0,img.Width,img.Height),
>> >> GraphicsUnit.Pixel)
>> >>
>> >> convertToGrayScale();
>> >>
>> >>
>> >>
>> >> "Naveen koul" <(E-Mail Removed)> wrote in message
>> >> news:B6AD9ACC-A511-4E34-8BC2-(E-Mail Removed)...
>> >> > No this is not the thing i want, i feel your are not able to get
>> >> > what i
>> >> > am
>> >> > really asking for.
>> >> >
>> >> > i have a function were image is scaled in .net frameworkthat
>> >> > function
>> >> > is
>> >> > written as this
>> >> > **********************************************************
>> >> > were scale is 1.0;
>> >> > scale = value;
>> >> > Bitmap scaledImg = new
>> >> > Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>> >> > convertToGrayScale();
>> >> >
>> >> > **********************************************************
>> >> > Now i want to write this function in comapct frame work . as from
>> >> > here
>> >> > the
>> >> > value of width and height goes to convertto grayscale function which
>> >> > is
>> >> > like
>> >> > this
>> >> >
>> >> >
>> >> >
>> >> > ************************************************************
>> >> >
>> >> >
>> >> > protected void convertToGrayScale()
>> >> > {
>> >> > Color col;
>> >> > int gray;
>> >> > for (int l = 0; l<scaledImg.Width; l++)
>> >> > for (int c = 0; c<scaledImg.Height; c++)
>> >> > {
>> >> > col = scaledImg.GetPixel(l,c);
>> >> > gray = (int)(0.32f * col.R + 0.5f * col.G + 0.18f* col.B);
>> >> > scaledImg.SetPixel(l,c,Color.FromArgb(gray, gray, gray));
>> >> >
>> >> > }
>> >> >
>> >> >
>> >> > }
>> >> > ***************************************************************
>> >> >
>> >> > if i am passing like this in compactframework
>> >> >
>> >> > **********************************************************
>> >> > Bitmap scaledImg = new Bitmap(img);
>> >> > *********************************************************
>> >> > the values passing to convert to gray function are wrong.
>> >> > so i hardly get correct results.
>> >> >
>> >> > So please teelme alternative for this a.
>> >> >
>> >> > Regards
>> >> >
>> >> > Naveen
>> >> >
>> >> > "Joseph Byrns" wrote:
>> >> >
>> >> >> Is this what you want?
>> >> >>
>> >> >>
>> >> >>
>> >> >> Dim b As New Bitmap(x, y)
>> >> >>
>> >> >> Dim g As Graphics = Graphics.FromImage(b)
>> >> >>
>> >> >> g.DrawImage(AnotherBitmap, destinationRectangle, SourceRectangle,
>> >> >> GraphicsUnit.Pixel)
>> >> >>
>> >> >>
>> >> >>
>> >> >> "Naveen koul" <(E-Mail Removed)> wrote in
>> >> >> message
>> >> >> news
613F3FA-A570-4EFB-9BE6-(E-Mail Removed)...
>> >> >> > Sir,
>> >> >> >
>> >
>> >> >> > I want to assign a scaled bitmap into another bit map
>> >> >> > how to do it.i have got a.net framework function which is working
>> >> >> > fime
>> >> >> > and
>> >> >> > i
>> >> >> > need an alternative for it to make it working for .net
>> >> >> > compactframework.
>> >> >> > ************************************************************
>> >> >> > scaledImg = new
>> >> >> > Bitmap(img,(int)(img.Width*scale),(int)(img.Height*scale));
>> >> >> >
>> >> >> > ************************************************************
>> >> >> >
>> >> >> > How can this be done.
>> >> >> >
>> >> >> > regards
>> >> >> >
>> >> >> > Naveem
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>
>>
>>