Kayıt Ol

Giriş

Şifremi Kaybettim

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

soru ekleme

Soru sormak için giriş yapmalısınız.

Giriş

Kayıt Ol

.........

C# Bitmap ile görüntü harmanlama

C# Bitmap ile görüntü harmanlama

Elektronik ortamda bulunan resimlerin renklerini  değiştirilebilir hale getireceğiz..Kırmızı, Yeşil ve Mavi renkleri ile harmanlama işlemini yapacağız.&, |, ^ operatörleri uygulanacaktır.Ekran Görüntüsü bu şekildedir.

 

deneme

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace BitmapShading
{
public partial class MainForm : Form
{
private Bitmap sourceBitmap = null;
private Bitmap blendBitmap = null;
private Bitmap resultBitmap = null;

public MainForm()
{
InitializeComponent();

cmbBlueBlendType.Items.Add(ExtBitmap.BitwiseBlendType.None);
cmbBlueBlendType.Items.Add(ExtBitmap.BitwiseBlendType.And);
cmbBlueBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Or);
cmbBlueBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Xor);
cmbBlueBlendType.SelectedIndex = 0;

cmbGreenBlendType.Items.Add(ExtBitmap.BitwiseBlendType.None);
cmbGreenBlendType.Items.Add(ExtBitmap.BitwiseBlendType.And);
cmbGreenBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Or);
cmbGreenBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Xor);
cmbGreenBlendType.SelectedIndex = 0;

cmbRedBlendType.Items.Add(ExtBitmap.BitwiseBlendType.None);
cmbRedBlendType.Items.Add(ExtBitmap.BitwiseBlendType.And);
cmbRedBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Or);
cmbRedBlendType.Items.Add(ExtBitmap.BitwiseBlendType.Xor);
cmbRedBlendType.SelectedIndex = 0;
}

private void btnSaveNewImage_Click(object sender, EventArgs e)
{
ApplyFilter(false);

if (resultBitmap != null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = “Specify a file name and file path”;
sfd.Filter = “Png Images(*.png)|*.png|Jpeg Images(*.jpg)|*.jpg”;
sfd.Filter += “|Bitmap Images(*.bmp)|*.bmp”;

if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileExtension = Path.GetExtension(sfd.FileName).ToUpper();
ImageFormat imgFormat = ImageFormat.Png;

if (fileExtension == “BMP”)
{
imgFormat = ImageFormat.Bmp;
}
else if (fileExtension == “JPG”)
{
imgFormat = ImageFormat.Jpeg;
}

StreamWriter streamWriter = new StreamWriter(sfd.FileName, false);
resultBitmap.Save(streamWriter.BaseStream, imgFormat);
streamWriter.Flush();
streamWriter.Close();

resultBitmap = null;
}
}
}

private void ApplyFilter(bool preview)
{
if (sourceBitmap == null || blendBitmap == null ||
picBlendPreview.Image == null ||
picSourcePreview.Image == null)
{
return;
}

if (preview == true)
{
Bitmap sourcePreview = (Bitmap)picSourcePreview.Image;

pictureBox1.Image = sourcePreview.BitwiseBlend((Bitmap)picBlendPreview.Image,
(ExtBitmap.BitwiseBlendType)cmbBlueBlendType.SelectedItem,
(ExtBitmap.BitwiseBlendType)cmbGreenBlendType.SelectedItem,
(ExtBitmap.BitwiseBlendType)cmbRedBlendType.SelectedItem);
}
else
{
resultBitmap = sourceBitmap.BitwiseBlend(blendBitmap,
(ExtBitmap.BitwiseBlendType)cmbBlueBlendType.SelectedItem,
(ExtBitmap.BitwiseBlendType)cmbGreenBlendType.SelectedItem,
(ExtBitmap.BitwiseBlendType)cmbRedBlendType.SelectedItem);
}
}

private void BlendTypeSelectedIndexChangedEventHandler(object sender, EventArgs e)
{
ApplyFilter(true);
}

private void LoadImageButtonClickedEventHandler(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = “Select an image file.”;
ofd.Filter = “Png Images(*.png)|*.png|Jpeg Images(*.jpg)|*.jpg”;
ofd.Filter += “|Bitmap Images(*.bmp)|*.bmp”;

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (sender == btnLoadSourceImage)
{
StreamReader streamReader = new StreamReader(ofd.FileName);
sourceBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream);
streamReader.Close();

picSourcePreview.Image = sourceBitmap.CopyToSquareCanvas(pictureBox1.Width);

}
else if (sender == btnLoadBlendImage)
{
StreamReader streamReader = new StreamReader(ofd.FileName);
blendBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream);
streamReader.Close();

picBlendPreview.Image = blendBitmap.CopyToSquareCanvas(pictureBox1.Width);
}

ApplyFilter(true);
}
}

private void picSourcePreview_Click(object sender, EventArgs e)
{

}

private void picPreview_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void pnlFilterControls_Paint(object sender, PaintEventArgs e)
{

}

private void picBlendPreview_Click(object sender, EventArgs e)
{

}

private void MainForm_Load(object sender, EventArgs e)
{

}

private void lblGreen_Click(object sender, EventArgs e)
{

}
}
}

ExtBitmap.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication17
{
public static class ExtBitmap
{
public static Bitmap CopyToSquareCanvas(this Bitmap sourceBitmap, int canvasWidthLenght)
{
float ratio = 1.0f;
int maxSide = sourceBitmap.Width > sourceBitmap.Height ?
sourceBitmap.Width : sourceBitmap.Height;

ratio = (float)maxSide / (float)canvasWidthLenght;

Bitmap bitmapResult = (sourceBitmap.Width > sourceBitmap.Height ?
new Bitmap(canvasWidthLenght, (int)(sourceBitmap.Height / ratio))
: new Bitmap((int)(sourceBitmap.Width / ratio), canvasWidthLenght));

using (Graphics graphicsResult = Graphics.FromImage(bitmapResult))
{
graphicsResult.CompositingQuality = CompositingQuality.HighQuality;
graphicsResult.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsResult.PixelOffsetMode = PixelOffsetMode.HighQuality;

graphicsResult.DrawImage(sourceBitmap,
new Rectangle(0, 0,
bitmapResult.Width, bitmapResult.Height),
new Rectangle(0, 0,
sourceBitmap.Width, sourceBitmap.Height),
GraphicsUnit.Pixel);
graphicsResult.Flush();
}

return bitmapResult;
}

public static Bitmap BitwiseBlend(this Bitmap sourceBitmap, Bitmap blendBitmap,
BitwiseBlendType blendTypeBlue, BitwiseBlendType
blendTypeGreen, BitwiseBlendType blendTypeRed)
{
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
sourceBitmap.Width, sourceBitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);

BitmapData blendData = blendBitmap.LockBits(new Rectangle(0, 0,
blendBitmap.Width, blendBitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

byte[] blendBuffer = new byte[blendData.Stride * blendData.Height];
Marshal.Copy(blendData.Scan0, blendBuffer, 0, blendBuffer.Length);
blendBitmap.UnlockBits(blendData);

int blue = 0, green = 0, red = 0;

for (int k = 0; (k + 4 < pixelBuffer.Length) &&
(k + 4 < blendBuffer.Length); k += 4)
{
if (blendTypeBlue == BitwiseBlendType.And)
{
blue = pixelBuffer[k] & blendBuffer[k];
}
else if (blendTypeBlue == BitwiseBlendType.Or)
{
blue = pixelBuffer[k] | blendBuffer[k];
}
else if (blendTypeBlue == BitwiseBlendType.Xor)
{
blue = pixelBuffer[k] ^ blendBuffer[k];
}

if (blendTypeGreen == BitwiseBlendType.And)
{
green = pixelBuffer[k + 1] & blendBuffer[k + 1];
}
else if (blendTypeGreen == BitwiseBlendType.Or)
{
green = pixelBuffer[k + 1] | blendBuffer[k + 1];
}
else if (blendTypeGreen == BitwiseBlendType.Xor)
{
green = pixelBuffer[k + 1] ^ blendBuffer[k + 1];
}

if (blendTypeRed == BitwiseBlendType.And)
{
red = pixelBuffer[k + 2] & blendBuffer[k + 2];
}
else if (blendTypeRed == BitwiseBlendType.Or)
{
red = pixelBuffer[k + 2] | blendBuffer[k + 2];
}
else if (blendTypeRed == BitwiseBlendType.Xor)
{
red = pixelBuffer[k + 2] ^ blendBuffer[k + 2];
}

if (blue < 0) { blue = 0; } else if (blue > 255)
{ blue = 255; }

if (green < 0) { green = 0; } else if (green > 255)
{ green = 255; }

if (red < 0) { red = 0; } else if (red > 255)
{ red = 255; }

pixelBuffer[k] = (byte)blue;
pixelBuffer[k + 1] = (byte)green;
pixelBuffer[k + 2] = (byte)red;
}

Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
resultBitmap.Width, resultBitmap.Height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

Marshal.Copy(pixelBuffer, 0, resultData.Scan0, pixelBuffer.Length);
resultBitmap.UnlockBits(resultData);

return resultBitmap;
}

public enum BitwiseBlendType
{
None,
Or,
And,
Xor
}
}
}

 

Zeynep ŞİT

Hakkında Zeynep ŞİT

Namık Kemal Üniversitesi

Yorum ( 1 )

  1. galiba gözüm bozuldu

Leave a reply

CAPTCHA
Refresh

*