Create new Image from 2 diferent images C# - c#

I am working in a function where I have two images, one of them is the backgroud and the other one is a QR code, I need create a new image using the background(image 1) and the QR image, but I need define the position of the QRCode Image.
like the bellow image,
best regards

using (Image background = Image.FromFile("background.jpg"))
using (Image qrCode = Image.FromFile("qrCode.jpg"))
using (Graphics graphics = Graphics.FromImage(background))
{
int x = 100;
int y = 100;
graphics.DrawImage(qrCode, x, y);
background.Save("result.jpg", ImageFormat.Jpeg);
}

Related

svg to jpg (or png) with white background color

I'm running the following c# code to convert a SVG file to jpg, however, the files created return with a black background. Is there a good way to create a white background?
Or another solution to create perfect images?
var svgDocument = Svg.SvgDocument.Open(file);
var width = svgDocument.Width;
var height = svgDocument.Height;
float factor = outputWidth / width;
int newHeight = (int) Math.Floor(factor * height);
svgDocument.ShapeRendering = SvgShapeRendering.Auto;
Bitmap bmp = svgDocument.Draw(outputWidth, newHeight);
bmp.Save(#"output.jpg", ImageFormat.Jpeg);

Converting An Array of Byte[] To A Bitmap

I have a collection of Bitmaps that are stored in a List<byte[]> object. I want to combine all those Bitmaps into one image (kinda like a paneramic) without converting them back to bitmaps. I've tried using a MemoryStream (see below) but it doesn't seem to work. I always run out of memory or the image comes out corrupted. I would appreciate any thoughts/advice on the matter.
List<byte[]> FrameList = new List<byte[]>();
using (MemoryStream ms = new MemoryStream())
{
for (int i = 0; i < FrameList.Count; i++)
{
//Does not work
ms.Write(FrameList[i], i * FrameList[i].Length, FrameList[i].Length);
//Does not work
ms.Write(FrameList[i], 0, FrameList[i].Length);
}
Bitmap img = (Bitmap)Bitmap.FromStream(ms);
img.Save("D:\\testing.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
The steps:
calculate the final image size as sum of size of the individual
images
create a Bitmap with the final size
get the Graphics object
draw all images using the Graphics object
save the final Bitmap
var final = new Bitmap(width, height); // calculated final size
using (var graphics = Graphics.FromImage(final))
{
graphics.DrawImage(...); // draw all partial images onto the final bitmap
}

c# drawing objects at once instead of using a loop

Hello i am trying to draw about 1000 images and between 10 to 100 rectangels and elipses.
But i need all of them to show up on screen only when they done loading(not in a loading screen but in a game or slideshow). so for example
texturegrass = MyApp.Properties.Resources.Grass
Rectangle[] rects;
recs = new Rectangle[1000]
for (int i = 0; i < rects.Length; i++)
{
g.DrawImage(texturegrass,rects[i]);
}
this is what i done so far but every rectangle is been drawn by it own what cause a flickering problam.
I have double bufferd the app.
I tried using parallel but the application keep crashing
I hope one of you guys can help me...
##*
You can use a Graphics object to create the image off-screen, and then draw the image on the screen using the screen's Graphics object.
var bmp = new Bitmap(MyWidth, CMyHeight);
var gOff = Graphics.FromImage(bmp);
gOff.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
texturegrass = MyApp.Properties.Resources.Grass
Rectangle[] rects = ...;
recs = new Rectangle[1000]
for (int i = 0; i < rects.Length; i++) {
gOff.DrawImage(texturegrass,rects[i]);
}
At this point you can draw bmp all at once on the screen's Graphic.
Microsoft: How to Draw Images Off-Screen

Change image size with C# in MVC3?

[HttpPost]
public ActionResult AddImage(Image model)
{
if (model.ImageData != null && model.ImageData.ContentLength > 0)
{
var fileName = Path.GetFileName(model.ImageData.FileName);
var pathBig = Path.Combine(Server.MapPath("~/UploadedImages"), fileName);
var pathSmall = Path.Combine(Server.MapPath("~/UploadedImages"), "small_" + fileName);
// --> How to change image size to big(800 x 600)
// and small (100x80) and save them?
model.ImageData.SaveAs(pathBig);
model.ImageData.SaveAs(pathSmall);
}
}
How do I change the image size to big(800 x 600) to small (100x80) and save them?
You could try this library:
http://nuget.org/packages/ImageResizer
It does support asp.net-mvc:
http://imageresizing.net/
Or you could get a pure C# lib and use it on your app. See these posts:
Resize an Image C#
https://stackoverflow.com/a/2861813/368070
And this snippet I found : http://snippets.dzone.com/posts/show/4336
The simplest way of doing it from the framework methods itself will be to use the DrawImage() method of the Graphics Class.
The example code could be like:
//For first scale
Bitmap bmp = new Bitmap(800, 600);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,800,600))
gf.Save(/* the save path */);
//For second scale
Bitmap bmp = new Bitmap(100, 80);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,100,80))
gf.Save(/* the save path */);

How do I superimpose one bitmap image on another in GDI+?

Using GDI+ I've made a heatmap bmp and I'd like to superimpose it on top of my bmp map. I've saved the two bmps to disk, and they look good, I just need a way to put them together. Is there any way to do this, perhaps using the Graphics object? How is transparency/alpa involved?
I'm very new to GDI programming so please be as specific as possible.
OK - here's an answer. At some point I need to learn how GDI+ works...
I couldn't get around the transarency issues, but this works. It just copies the non-white pixels from the overlay to the map:
for (int x = 0; x < map.Width; x++)
for (int y = 0; y < map.Height; y++) {
Color c = overlay.GetPixel(x, y);
if ((c.A != 255) || (c.B != 255) || (c.G != 255) || (c.R != 255))
map.SetPixel(x, y, c);
This should do the job...
At the moment the Image you want to superimpose onto the main image will be located in the top left corner of the main Image, hence the new Point(0,0). However you could change this to locate the image anywhere you want.
void SuperimposeImage()
{
//load both images
Image mainImage = Bitmap.FromFile("PathOfImageGoesHere");
Image imposeImage = Bitmap.FromFile("PathOfImageGoesHere");
//create graphics from main image
using (Graphics g = Graphics.FromImage(mainImage))
{
//draw other image on top of main Image
g.DrawImage(imposeImage, new Point(0, 0));
//save new image
mainImage.Save("OutputFileName");
}
}

Categories

Resources