how to assign a scaled bitmap into another bitmap(

G

Guest

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
 
J

Joseph Byrns

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)
 
G

Guest

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
 
J

Joseph Byrns

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();
 
G

Guest

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));
 
S

Sergey Bogdanov

There is no need to ask the same question twice. Christian Schwarz gave
you the idea yesterday. Here you are the complete list:

Bitmap scaledImg = ScaleBitmap(sourceBitmap, 0.5f, 0.5f);

....


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;
}
 
G

Guest

Sir,

Yes there was no question of posting the post twice , but the question in
both post is different . in first post i forgot to mention that i dont want
to draw image . I want to copy the bitmap ,
Again i am repeating the question. i dont want to draw the image i want to
copy the bitmap.

i hope my query will be bit clear now.

Regards

Naveen
There is no need to ask the same question twice. Christian Schwarz gave
you the idea yesterday. Here you are the complete list:

Bitmap scaledImg = ScaleBitmap(sourceBitmap, 0.5f, 0.5f);

....


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;
}

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Naveen said:
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));
:
 
S

Sergey Bogdanov

The code snippet from the previous post does exactly what you are
looking for. Just try to invoke this method - it will return a copy of
Bitmap.

I believe, I understand where you stick. The Graphics.FromImage returns
something like of "Device Context" for a given Bitmap. This context is
used for copying one Bitmap to another by using the Draw method. It
doesn't display a Bitmap, it just makes an copy.

Again, just test it.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Naveen said:
Sir,

Yes there was no question of posting the post twice , but the question in
both post is different . in first post i forgot to mention that i dont want
to draw image . I want to copy the bitmap ,
Again i am repeating the question. i dont want to draw the image i want to
copy the bitmap.

i hope my query will be bit clear now.

Regards

Naveen

There is no need to ask the same question twice. Christian Schwarz gave
you the idea yesterday. Here you are the complete list:

Bitmap scaledImg = ScaleBitmap(sourceBitmap, 0.5f, 0.5f);

....


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;
}

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Naveen said:
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));
:



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();





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

:



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)





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
 
J

Joseph Byrns

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(...)
 
G

Guest

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
 
J

Joseph Byrns

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top